java - Add StartElement to JAXBContext marshal -
java - Add StartElement to JAXBContext marshal -
i have object x want marshal file.
xml of x :
<x id="0"><f1>bla</f1><f2>bla </f2><f3>bla</f3></x>
this part figured out ... if have list of x's want write file.
but want start file
<z> <y> <k> <x id="1"> ... </x> <x id="2"> ... </x> <x id="3"> ... </x> </k> </y> </z>
is there anyway can marshel ?! add together z y , k list of x's , close z y , k ?!
thank ...
you leverage jaxb , stax , following:
import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbelement; import javax.xml.bind.marshaller; import javax.xml.namespace.qname; import javax.xml.stream.xmloutputfactory; import javax.xml.stream.xmlstreamwriter; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(x.class); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_fragment, true); xmloutputfactory xof = xmloutputfactory.newfactory(); xmlstreamwriter xsw = xof.createxmlstreamwriter(system.out); xsw.writestartdocument(); xsw.writestartelement("x"); xsw.writestartelement("y"); xsw.writestartelement("k"); x x1 = new x(); x1.setid(1); jaxbelement<x> jaxbelement1 = new jaxbelement<x>(new qname("x1"), x.class, x1); marshaller.marshal(jaxbelement1, xsw); x x2 = new x(); x2.setid(1); jaxbelement<x> jaxbelement2 = new jaxbelement<x>(new qname("x2"), x.class, x2); marshaller.marshal(jaxbelement2, xsw); x x3 = new x(); x3.setid(3); jaxbelement<x> jaxbelement3 = new jaxbelement<x>(new qname("x3"), x.class, x3); marshaller.marshal(jaxbelement3, xsw); xsw.writeendelement(); xsw.writeendelement(); xsw.writeendelement(); xsw.writeenddocument(); } }
output
<?xml version='1.0' encoding='utf-8'?><x><y><k><x1 id="1" /><x2 id="1" /><x3 id="3" /></k></y></x>
formatting
to formatting do:
import javax.xml.bind.jaxbcontext; import javax.xml.bind.jaxbelement; import javax.xml.bind.marshaller; import javax.xml.namespace.qname; import javax.xml.stream.xmloutputfactory; import javax.xml.stream.xmlstreamwriter; public class demo { public static void main(string[] args) throws exception { jaxbcontext jc = jaxbcontext.newinstance(x.class); marshaller marshaller = jc.createmarshaller(); marshaller.setproperty(marshaller.jaxb_fragment, true); xmloutputfactory xof = xmloutputfactory.newfactory(); xmlstreamwriter xsw = xof.createxmlstreamwriter(system.out); xsw.writestartdocument(); xsw.writecharacters("\n"); xsw.writestartelement("x"); xsw.writecharacters("\n\t"); xsw.writestartelement("y"); xsw.writecharacters("\n\t\t"); xsw.writestartelement("k"); x x1 = new x(); x1.setid(1); x1.setf1("x1f1"); jaxbelement<x> jaxbelement1 = new jaxbelement<x>(new qname("x1"), x.class, x1); xsw.writecharacters("\n\t\t\t"); marshaller.marshal(jaxbelement1, xsw); x x2 = new x(); x2.setid(1); x2.setf1("x2f1"); jaxbelement<x> jaxbelement2 = new jaxbelement<x>(new qname("x2"), x.class, x2); xsw.writecharacters("\n\t\t\t"); marshaller.marshal(jaxbelement2, xsw); x x3 = new x(); x3.setid(3); x3.setf1("x3f1"); jaxbelement<x> jaxbelement3 = new jaxbelement<x>(new qname("x3"), x.class, x3); xsw.writecharacters("\n\t\t\t"); marshaller.marshal(jaxbelement3, xsw); xsw.writecharacters("\n\t\t"); xsw.writeendelement(); xsw.writecharacters("\n\t"); xsw.writeendelement(); xsw.writecharacters("\n"); xsw.writeendelement(); xsw.writeenddocument(); } }
output
<?xml version='1.0' encoding='utf-8'?> <x> <y> <k> <x1 id="1"><f1>x1f1</f1></x1> <x2 id="1"><f1>x2f1</f1></x2> <x3 id="3"><f1>x3f1</f1></x3> </k> </y> </x>
java xml jaxb marshalling xml-formatting
Comments
Post a Comment