I'm implementing a homebrew subprotocol of XMPP, and i'm using combination of StAX and JAXB for parsing/marshalling mesages. An开发者_开发知识库d 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 ;)
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.
精彩评论