0

I have a problem with JAXB marshalling. I have a set of classes generated from some XSD. I need to be able to change the namespace URI dynamically - when marshalling an object.

I simply used my own QName, like

new JAXBElement<MyType>(new QName(uri, "Doc") ....

and it works. However the problem is that MyType composes of few other classes, and I would like to change namespace for them as well (in fact all of those elements use one and the same namespace, and I only want to change the URI of this namespace). Resulting XML changes namespace only for my "root" element, the one I specified QName for, it looks like this

<ns2:Dok>
    <ns1:Elem>a</ns1:Elem>
</ns2:Dok>

Where ns2 is new namespace I supplied with QName, but ns1 is still the "old one", if written in package info for generated classes). My question is - how to change the namespace for good? How to change a namespace URI for all the elements, how to make my new QName to apply down to all child elements as well?

M4ks
  • 11,744
  • 7
  • 27
  • 48

1 Answers1

0

You can set a default namespace on your root element. By definition then, if you do not supply a namespace for an inner (child) element it will inherit the default namespace that the parent has declared. In your case, declare ns2 as the default namespace on and then you have to remove the ns1 namespace from . You might also find this question/anwers Removing extra ns2 annotation in xml useful for your case.

Community
  • 1
  • 1
cpard
  • 330
  • 1
  • 7