Reputation: 111
I need to make an xml like below using JAXB, But i can't get <front>
and <back>
under the <result>
tag.
<?xml version="1.0" encoding="UTF-8"?>
<output>
<option>abc</option>
<refid>8789</refid>
<response>
<responsecode>1234</responsecode>
<responsedetails>xyz</responsedetails>
</response>
<result>
<front>
<containimage>Yes</ontainimage>
<containdetail>No</containdetail>
</front>
<back>
<ontainimage>Yes</ontainimage>
<containdetail>Yes</containdetail>
</back>
</result>
</output>
I am able to get the <response>
but not the <result>
I am using something like -
@XmlElementWrapper(name = "result")
@XmlElement
public ArrayList<Front> getFront() {
return front;
}
@XmlElementWrapper(name = "result")
@XmlElement
public ArrayList<Back> getBack() {
return back;
}
and get the xml as
<?xml version="1.0" encoding="UTF-8"?>
<output>
<option>abc</option>
<refid>8789</refid>
<response>
<responsecode>1234</responsecode>
<responsedetails>xyz</responsedetails>
</response>
<result>
<front>
<containimage>Yes</ontainimage>
<containdetail>No</containdetail>
</front>
</result>
<result>
<back>
<ontainimage>Yes</ontainimage>
<containdetail>Yes</containdetail>
</back>
</result>
</output>
I need <front>
and <back>
inside <result>
. Please help
Upvotes: 0
Views: 478
Reputation: 1143
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:
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à :)
Upvotes: 1