By default, jaxb 2 lists all (all possible required) namespaces in r开发者_运维百科oot 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.
The answer is negative, JAXB does not support such low-level customization. Consider using XSLT to post-process the marshalled XML.
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.
精彩评论