java - JAXB marshalling many objects to one file -



java - JAXB marshalling many objects to one file -

i want marshal many objects single xml file. going well, except marshaller insists on adding <?xml version="1.0" ?> before each object.

what's preferred way marshal many objects same file? if nil else, what's best way rid of these extraneous xml declarations?

my current code:

jaxbcontext jc = jaxbcontext.newinstance(relevantclasses); marshaller m = jc.createmarshaller(); xmloutputfactory xof = xmloutputfactory.newfactory(); xmlstreamwriter xsw = xof.createxmlstreamwriter(channels.newoutputstream(filewritechannel), "utf-8"); xsw.writestartdocument("utf-8", "1"); m.marshal(object1, xsw); m.marshal(object2, xsw); xsw.close();

this works great, , <object1> , <object2> info expect... has additional <?xml version="1.0" ?> before each object.

an xml document has 1 root element, marshalling several objects single file won't lead valid xml.

you should have 1 root object object1 element, , object2 element, , marshal root object.

otherwise, marshaller api doc says:

supported properties

[...]

jaxb.fragment - value must java.lang.boolean property determines whether or not document level events generated marshaller. if property not specified, default false. property has different implications depending on marshal api using - when property set true:

[...]

marshal(object,xmlstreamwriter) - marshaller not generate xmlstreamconstants.start_document , xmlstreamconstants.end_document events.

java xml xml-serialization jaxb marshalling

Comments

Popular posts from this blog

delphi - blogger via idHTTP : error 400 bad request -

c++ - compiler errors when initializing EXPECT_CALL with function which has program_options::variables_map as parameter -

How do I check if an insert was successful with MySQLdb in Python? -