I Have a problem with marshalling the code as expected XML
public void xmleg() throws XMLStreamException
{
XMLOutputFactory factory = XMLOutputFactory.newInstance();
XMLStreamWriter writer = factory.createXMLStreamWriter(System.out);
writer.writeStartDocument();
writer.writeStartElement("Zoos1");
QName q=new QName("","Zoo");
for(Zoo add: zoo_list)
{
try
{
JAXBContext jaxbContext = JAXBContext.newInstance(Zoos.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
jaxbMarshaller.marshal(new JAXBElement<Zoot>(q,Zoo.class,add),System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
writer.writeEndDocument();
writer.close();
}
}
Have used the XMLStreamWriter to print the tags required as default..But all the write statements are printed at the last Start tag,start element as well.
The generated output is:
<Zoo>
<linkId>0</linkId>
<name>fjjfjfrj</name>
</Zoo>
<Zoo>
<linkId>0</linkId>
<name>fgjfjfj</name>
</Zoo>
<?xml version="1.0" ?><Zoos></Zoos>
The expected output should be:
<?xml version="1.0" ?>
<Zoo>
<linkId>0</linkId>
<name>fjjfjfrj</name>
</Zoo>
<Zoo>
<linkId>0</linkId>
<name>fgjfjfj</name>
</Zoo>