开发者

Duplicate namespace declarations in JAXB generated XML

开发者 https://www.devze.com 2022-12-18 04:51 出处:网络
I am using JAXB to generate XML from Java objects, it\'s a realtime, quite high message rate applic开发者_高级运维ation and works fine most of the time.However occassionally and without any obvious cl

I am using JAXB to generate XML from Java objects, it's a realtime, quite high message rate applic开发者_高级运维ation and works fine most of the time. However occassionally and without any obvious clues as to why, I am getting duplicate namespace declarations in the generated XML. eg:

<UpdateRequest xmlns="http://xml.mycomp.com/ns/myservice" 
               xmlns="http://xml.mycomp.com/ns/myservice">
    <field1>value</field1>
    ...
</UpdateRequest>

Has anyone seen this behaviour before?


Check if the xsd code of this class allow the creation of more than 1 instance of the repeated attribute. if so, you can avoid this repetitions setting the number of instances of the xmlns attribute for each UpdateRequest object.

If the problem is your code (maybe there is being created this attribute twice) and you have limited the number of instances of the attribute (as i said above), the program will show an error at runtime complaining that you are trying to insert an attribute already defined.


A solution might be available at this link.

here's the relevant section quoted verbatim from the above link that may be relevant for you:

Similar explicit inclusion of a schema type in an instance document's element occurs if you instantiate a JAXB element using an object of some (abstract) XML schema base type so that the element would have the element tag of the base type.

Second, avoid xs:anySimpleType since this will also create multiple references to the namespaces bound to xsi and xs, and type attributes containing the actual type. And you lose JAXB's advantage of having typed fields in your Java classes so that you lose all the checks the Java compiler might do, and for unmarshalling you'll have to handle all the conversions yourself.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号