1
我和編組代碼預期XML問題JAXB輸出格式不正確
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();
}
}
是否使用了XMLStreamWriter打印所需的default..But標籤的所有語句寫在最後的印刷開始標記,開始元素。
所生成的輸出是:
<Zoo>
<linkId>0</linkId>
<name>fjjfjfrj</name>
</Zoo>
<Zoo>
<linkId>0</linkId>
<name>fgjfjfj</name>
</Zoo>
<?xml version="1.0" ?><Zoos></Zoos>
預期輸出應該是:
<?xml version="1.0" ?>
<Zoo>
<linkId>0</linkId>
<name>fjjfjfrj</name>
</Zoo>
<Zoo>
<linkId>0</linkId>
<name>fgjfjfj</name>
</Zoo>