JAXB appending unneeded namespace declarations to tags

天涯浪子 提交于 2019-12-04 10:47:34

问题


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 ;)


回答1:


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.



来源:https://stackoverflow.com/questions/1673626/jaxb-appending-unneeded-namespace-declarations-to-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!