JAXB生成的XML中的重复名称空间声明 [英] Duplicate namespace declarations in JAXB generated XML

查看:88
本文介绍了JAXB生成的XML中的重复名称空间声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JAXB从Java对象生成XML,这是一个实时的,消息速率很高的应用程序,并且在大多数情况下都可以正常工作.但是偶尔,也没有任何明显的原因,我在生成的XML中得到重复的名称空间声明.例如:

I am using JAXB to generate XML from Java objects, it's a realtime, quite high message rate application 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?

推荐答案

以下是上述链接中逐字引用的相关部分,可能与您相关:

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

相似地明确包含模式 输入实例文档的元素 实例化JAXB时发生 使用某些对象的元素 (抽象)XML模式基础类型 该元素将具有 基本类型的元素标签.

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.

第二,避免使用xs:anySimpleType,因为 这还将创建多个 对绑定到的名称空间的引用 xsi和xs,并键入属性 包含实际类型.和你 失去了JAXB打字的优势 Java类中的字段 您会丢失所有Java支票 编译器可能会这样做,并且对于 解组您将不得不处理 自己进行所有转化.

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.

这篇关于JAXB生成的XML中的重复名称空间声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆