JAXB appending unneeded namespace declarations to tags
Asked Answered
R

1

12

I'm implementing a homebrew subprotocol of XMPP, and i'm using combination of StAX and JAXB for parsing/marshalling mesages. And when I marshall a message I end up with loads of unneded namespace declarations:

   <ns2:auth xmlns:ns2="urn:ietf:params:xml:ns:ilf-auth" 
   xmlns:ns4="ilf:iq:experiment:power" xmlns:ns3="ilf:iq:experiment:init" 
   xmlns:ns5="ilf:iq:experiment:values" xmlns:ns6="ilf:iq:experiment:result" 
   xmlns:ns7="ilf:iq:experiment:stop" xmlns:ns8="ilf:iq:experiment:end">
   compton@ilf</ns2:auth>

instead of:

   <ns:auth xmlns:ns="urn:ietf:params:xml:ns:ilf-auth>compton@ilf</ns:auth>

Is there any way to turn that of?

All these namespaces are used in different messages that get marshalled/unmarshalled by JAXB, but every message uses one namespace.

PS. I am not an XML expert please dont rant me if I did some stupid mistake ;)

Rubalcava answered 4/11, 2009 at 13:0 Comment(2)
This is completely unaccepptable for legal documents that must be preserved during transport to maintain their integrity. JAXB is a way to make simple things more complexArgonaut
JAXB makes xml parsing/producing way easier. I did a project that is uses both JAXB and dom4j (in two different parts) and difference is big.Rubalcava
S
12

The functionality you are looking for was requested as an enhancement on the JAXB issue tracker in issue 103. The enhancement was declined, since the JAXB authors find it too expensive to traverse the object tree once more before the actual serialization starts to determine which namespaces are actually required.

Although rather bloated, the unnecessary namespaces are not invalidating the XML document. If you really have to save the few bytes and the extra computation cost is affordable, it should be quite easy to write your own XML processor/filter, which takes the JAXB output, parses the document and writes a new document without the unused namespace definitions.

Scale answered 4/11, 2009 at 14:26 Comment(1)
XSLT to perform this transformation, and if you happen to be using CXF: cxf.apache.org/docs/xslt-feature.htmlMashhad

© 2022 - 2024 — McMap. All rights reserved.