Question

When using JAXB marshalling, do I have an influence on which element a namespace/namespace prefix will be declared?

Currently, all namespace prefixes are declared at the root element, but due to strange limitations of the system which processes my XML I need to declare them at child elements (which would still result in a valid xml document).

A similar, but not identical request has been made to the official jaxb issue tracker and declined by a developer back in 2006. I'd like to know if this situation changed in the meanwhile or if some workarounds exist.

Any help is appreciated.

Example:

JAXB marshalling creates the following XML:

<outer xmlns:ns1="http://mydomain">
  <inner>
     <ns1:data/>
  </inner>
</outer>

While I need to have something like (ns1 prefix is not declared at the root element):

<outer>
  <inner xmlns:ns1="http://mydomain">
     <ns1:data/>
  </inner>
</outer>
Was it helpful?

Solution

JAXB (JSR-222) does not provide a means to control where namespace declarations occur. JAXB providers tend to put the namespaces on the root element (for performance reasons), but they are not required to.

Below is a link to an answer I gave to a similar question where XMLStreamWriter is extended to control when the namespace declarations get reported.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top