users@jaxb.java.net

JAXB 2.1.3: customize namespaces

From: ivan <ivan_at_dhousestudio.com>
Date: Tue, 02 Sep 2008 16:12:18 +0300

   Hi!

I use JAXB 2.1.3 for generation of XML-files from java object model.
I have some object A and object B. B is inside A.
So, after generation I get following:


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://my.namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <SOAP-ENV:Body>
         <ns1:myTag>
        ...
         </ns1:myTag>
     </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

In my example object A is Envelope, object B is myTag.
I use code:

                Marshaller marshaller = context.createMarshaller();
                marshaller.setProperty("jaxb.encoding", "UTF-8");
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",
                                new NamespacePrefixMapperImpl());

                marshaller.marshal(myObject, outputStream);


In NamespacePrefixMapperImpl I override getPreferredPrefix() and
getPreDeclaredNamespaceUris().


I would write namespace from object B inside tag <myTag>, but not in
root tag.
I mean following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
     <SOAP-ENV:Body>
         <ns1:myTag xmlns:ns1="http://my.namespace">
        ...
         </ns1:myTag>
     </SOAP-ENV:Body>
</SOAP-ENV:Envelope>


How can I do it?



   Thanks in advance!
   Truly yours,
   Ivan Pryvalov.