Duplicate namespace declarations in JAXB generated XML
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.
精彩评论