I suppose you problem is in the Output class, or by addind a new result instance to our output object... By the way, do you manually create the java binding, or lat JaxB generates it from an the schema?
I propose you the following steps:
- create a schema (XSD file) that match your XML
- let JaxB generate the java binding
- write the File
For the Schema, I created the following one (correcting the spelling of the tag "containimage")
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="output">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="option" type="xsd:string" />
<xsd:element name="refid" type="xsd:string" />
<xsd:element name="response">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="responsecode" type="xsd:string" />
<xsd:element name="responsedetails" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="result" maxOccurs="1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="front" type="resultType" />
<xsd:element name="back" type="resultType" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="resultType">
<xsd:sequence>
<xsd:element name="containimage" type="xsd:string" />
<xsd:element name="containdetail" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
Then, I asked JaxB generate the Java binding (wizzard in Eclipse), that generates me 3 classes: ObjectFactory, Output and ResultType classes.
Finally, I can write the output file:
package stackoverflow35769423;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import stackoverflow35769423.jaxb.ObjectFactory;
import stackoverflow35769423.jaxb.Output;
import stackoverflow35769423.jaxb.ResultType;
public class CreateResultFile {
public static void main(String[] args) {
try {
(new CreateResultFile()).writeFile();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
private void writeFile() throws JAXBException, FileNotFoundException {
OutputStream os = new FileOutputStream("files" + File.separator + "output.xml");
ObjectFactory factory = new ObjectFactory();
JAXBContext jaxbContext = JAXBContext.newInstance(factory.getClass().getPackage().getName());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Output output = factory.createOutput();
output.setOption("abc");
output.setRefid("8789");
Output.Response reponse = factory.createOutputResponse();
reponse.setResponsecode("1234");
reponse.setResponsedetails("xyz");
output.setResponse(reponse);
Output.Result result = factory.createOutputResult();
ResultType resultFront = factory.createResultType();
resultFront.setContainimage("Yes");
resultFront.setContaindetail("No");
ResultType resultBack = factory.createResultType();
resultBack.setContainimage("Yes");
resultBack.setContaindetail("Yes");
result.setFront(resultFront);
result.setBack(resultBack);
output.setResult(result);
jaxbMarshaller.marshal(output, os);
}
}
Et Voilà :)
resultPOJO that contains both afrontandbackPOJO