Create xml from two different xsd
HelloI need to solve how to create a String of XML from two different xsd:s.
The result need to look like this snippet:
<xsdone:Payloadinformation>
<xsdone:Payload>
<xsdtwo:AObjectElement xmlns:xsdtwo="com/company/xsdtwo/v1">
<xsdtwo:ChildElementOne>123</xsdtwo:ChildElementOne>
<xsdtwo:ChildElementTwo>ABC</xsdtwo:ChildElementTwo>
</xsdtwo:AObjectElement>
</xsdone:Payload>
</xsdone:Payloadinformation>
The xsdone looks like this
<xs:complexType name="PayloadinformationTYPE">
<xs:sequence>
<xs:element name="Payload" type="xs:anyType" minOccurs="1" maxOccurs="1">
</xs:element>
</xs:sequence>
</xs:complexType>
As you can see it uses xs:anyType
The xsdtwo looks like this
<xs:complexType name="AObjectElementTYPE">
<xs:sequence>
<xs:element name="ChildElementOne" type="xs:int" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
To finally get the xml-string I use JaxB like:
public static String getXmlStringForPayloadinformationTYPE(PayloadinformationTYPE payloadinformationTYPE) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(PayloadinformationTYPE.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<PayloadinformationTYPE> jaxbElement =
new JAXBElement<>(new QName("", "xsdone:PayloadinformationTYPE"),
PayloadinformationTYPE.class,
payloadinformationTYPE);
StringWriter lStringWriter = new StringWriter();
jaxbMarshaller.marshal(jaxbElement, lStringWriter);
return lStringWriter.toString();
}
The problem might be in the above code, since it does not got any info about xsdtwo, I guess.
To set the Payload, the generated method in PayloadinformationTYPE looks like:
public void setPayload( Object value )
As you can see it does not map to any xsd-generated class, just any instance of Object.
If I just create a AObjectElementTYPE and set it straight away as payload like
AObjectElementTYPE aObjectElementTYPE = getAObjectElementTYPE();
payloadinformationTYPE.setPayload(aObjectElementTYPE);
I get
<xsdone:Payloadinformation>
<xsdone:Payload>com.company.xsdtwo.v1.AObjectElementTYPE@cb0755b</xsdone:Payload>
As you see the payload is not as I expected.
I have also tried to set it like a String:
AObjectElementTYPE aObjectElementTYPE = getAObjectElementTYPE();
String lXmlStringForAObjectElementTYPE = getXmlStringForAObjectElementTYPE(aObjectElementTYPE); //This method looks in the same way as getXmlStringForPayloadinformationTYPE above
payloadinformationTYPE.setPayload(lXmlStringForAObjectElementTYPE);
But then I get the xml like this:
<xsdone:Payloadinformation>
<xsdone:Payload xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="xsd:string"><?xml version="1.0" encoding="UTF-8"?>
<AObjectElementTYPE xmlns="com/company/xsdbas/v1" xmlns:xsdtwo="com/company/xsdtwo/v1">
<xsdtwo:ChildElementOne>1</xsdtwo:ChildElementOne>
</AObjectElementTYPE>
</xsdone:Payload>
As you can see that is not what I excpected either.
My last, and best attempt so far is to
AObjectElementTYPE aObjectElementTYPE = getAObjectElementTYPE();
Node lXmlNodeForAObjectElementTYPE = getXmlNodeForAObjectElementTYPE(aObjectElementTYPE);
payloadinformationTYPE.setPayload(lXmlNodeForAObjectElementTYPE);
...
public static Node getXmlNodeForAObjectElementTYPE(AObjectElementTYPE aObjectElementTYPE) throws JAXBException, ParserConfigurationException, IOException, SAXException {
JAXBContext jaxbContext = JAXBContext.newInstance(AObjectElementTYPE.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
JAXBElement<AObjectElementTYPE> jaxbElement =
new JAXBElement<>(new QName("", "xsdtwo:AObjectElementTYPE"),
AObjectElementTYPE.class,
aObjectElementTYPE);
StringWriter lStringWriter = new StringWriter();
jaxbMarshaller.marshal(jaxbElement, lStringWriter);
DocumentBuilderFactory lDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder lDocumentBuilder = lDocumentBuilderFactory.newDocumentBuilder();
String lXmlString = lStringWriter.toString();
lXmlString = lXmlString.substring( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n".length(), lXmlString.length() );
Element node = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.parse(new ByteArrayInputStream(lXmlString.getBytes()))
.getDocumentElement();
return node;
}
Then I get
<xsdone:Payloadinformation>
<AObjectElementTYPE xmlns="">
<xsdtwo:ChildElementOne xmlns="">123</xsdtwo:ChildElementOne>
</AObjectElementTYPE>
As you can see the tag Payload is missing and the value for attribute xmlns is missing?
The expected output should have been like:
<xsdone:Payloadinformation>
<xsdone:Payload>
<xsdtwo:AObjectElement xmlns:xsdtwo="com/company/xsdtwo/v1">
<xsdtwo:ChildElementOne>123</xsdtwo:ChildElementOne>
Do you guys got any better way to build the XML I need?
Best regards
Fredrik
PS I seem to forgot how to use bb-codes for code. I thought it was [code] ..[/code]