jaxb namespaces in each element instead of root element during marshalling

自闭症网瘾萝莉.ら 提交于 2019-12-25 03:38:29

问题


By default, jaxb 2 lists all (all possible required) namespaces in root element during marshalling:

<rootElement xmlns="default_ns" xmlns:ns1="ns1" xmlns:ns2="ns2"> <ns1:element/> </rootElement>

Is there a way to describe namespace in each element instead of root element ?:

<rootElement xmlns="default_ns"> <element xmlns="ns1"/> </rootElement>

It also solves the problem of "unnecessary namespaces", which is also important in my case. Any suggestions appreciated.


回答1:


The answer is negative, JAXB does not support such low-level customization. Consider using XSLT to post-process the marshalled XML.




回答2:


Also, I tend to agree wtih @Anton in the response he offered in the other thread. I really shouldn't matter. And if you must force one way or the other, you should always prefer the top-level context, especially if you have potentially repeated elements. Repeating the namespace declaration in sibling elements is a waste of space as well as sub-optimal for the parser at the receiving end.



来源:https://stackoverflow.com/questions/2385523/jaxb-namespaces-in-each-element-instead-of-root-element-during-marshalling

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