1

I have an XSD Schema which uses namespaces to make elements with the same name distinguishable. I am generating the JAXB bindings using the Maven plugin jaxb2-maven-plugin.

When marshalling my JAXB object, JAXB substitutes every namespace with an identifier in the root node. The XML is still valid, but the readability (or recognizability) for the user of my application suffers.

What it should look like:

<Root xmlns="http://somenamespace.com/Root">
    <Config xmlns="http://somenamespace.com/ConfigTypeOne"/>
    <Config xmlns="http://somenamespace.com/ConfigTypeTwo"/>    
</Root>

What it looks like after marshalling:

<Root xmlns="http://somenamespace.com/Root" xmlns:ns1="http://somenamespace.com/ConfigTypeOne" xmlns:ns2="http://somenamespace.com/ConfigTypeTwo">
    <ns1:Config/>
    <ns2:Config/>    
</Root>

Is there a way to tell JAXB to skip the substitution of namespaces and marshall the XML the way it's defined in the XSD schema?

padde
  • 661
  • 8
  • 19
  • Check out this answer I gave to a similar question: http://stackoverflow.com/questions/5720501/jaxb-marshalling-xmpp-stanzas/5722013#5722013 – bdoughan May 13 '13 at 13:25
  • Thanks, that works! Only problem is that the Marshaller.JAXB_FORMATTED_OUTPUT property seems to be ignored totally. The XML is written without indentation. Is there a way to achieve this? – padde May 13 '13 at 13:40
  • The answers to the following question may help: http://stackoverflow.com/questions/290326/stax-xml-formatting-in-java – bdoughan May 13 '13 at 13:56
  • Thanks again. But this doesn't really help in my case. The question seems to be for StAX and the answers seems to be something equivalent to the JAXB_FORMATTED_OUTPUT property of JAXB. Unfortunately the approach with the CustomXMLStreamWriter seems to be affecting this property. – padde May 14 '13 at 12:58
  • The `JAXB_FORMATTED_OUTPUT` property does not apply when marshalling to an `XMLStreamWriter`. However the first link I gave you leverages an `XMLStreamWriter` to solve the namespace issue and the second link addresses formatting, so combined they should give you the answer you are looking for. – bdoughan May 14 '13 at 13:08

0 Answers0