java - XmlstreamWriter and Jaxb for subtree w/o namespace -


i using xmlstreamwriter , jaxb in conjunction marshall big xml file. creating sub-tree jaxb issue when marshal sub-tree prefixed default namespace below:

<?xml version="1.0" encoding="utf-8"?> <messagemodel xmlns="urn:schemas.mycompany.com/ent/messagemodel/2013/09/19">    <messageheader xmlns="" xmlns:ns2="urn:schemas.mycompany.com/ent/messagemodel/2013/09/19">       <ns2:providerid>5922</ns2:providerid>       <ns2:effectivedt>2016-08-08-04:00</ns2:effectivedt>       <ns2:partycount>0</ns2:partycount>       <ns2:arrangementcount>1</ns2:arrangementcount>       <ns2:appmetadatastring>ter</ns2:appmetadatastring>    </messageheader> </messagemodel> 

i using below code marshaling:

        stringwriter result = new stringwriter();         messageheadertype messageheadertype = createmessageheader(objectfactory);         jaxbelement<messageheadertype> element = new jaxbelement<messageheadertype>(new qname("messageheader"), messageheadertype.class, messageheadertype);          xmlstreamwriter  xmlout = xmloutputfactory.newfactory().createxmlstreamwriter(result);         **//setting default namespace**          xmlout.setdefaultnamespace("urn:schemas.mycompany.com/ent/messagemodel/2013/09/19");         xmlout.writestartdocument();         xmlout.writestartelement("urn:schemas.mycompany.com/ent/messagemodel/2013/09/19", "messagemodel");         xmlout.writenamespace("", "urn:schemas.mycompany.com/ent/messagemodel/2013/09/19");          jaxbcontext context = jaxbcontext.newinstance(messageheadertype.class);         marshaller marshaller = context.createmarshaller();         //marshaller.setproperty(marshaller.jaxb_formatted_output, boolean.true);         marshaller.setproperty(marshaller.jaxb_fragment, boolean.true);         marshaller.marshal(element, xmlout);           xmlout.writeenddocument();         xmlout.close();         system.out.println(result.tostring()); 

i setting default namespace still creating sub-tree namespace. can generate sub-tree jaxb without namespace?

the way used qname incorrect, had similar problem when first used it.

when give localname (one string constructor), assumes namespace empty. why messageheader has tag xmlns="". read more here.

the constructor should use this.

replace current qname 1 , should work:

new qname("urn:schemas.mycompany.com/ent/messagemodel/2013/09/19", "messageheader"); 

also, don't need

xmlout.writenamespace("", "urn:schemas.mycompany.com/ent/messagemodel/2013/09/19"); 

it's same thing default namespace one.


Comments