我有一个 JAXB 设置,我在其中使用 @XmlJavaTypeAdapter 将 Person
类型的对象替换为仅包含该人的 UUID 的 PersonRef
类型的对象。这工作得很好。但是,生成的 XML 每次使用时都会重新声明相同的命名空间 (xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
)。虽然这通常没问题,但就是感觉不对。
如何配置 JAXB 以在文档的开头声明 xmlns:xsi?我可以手动将命名空间声明添加到根元素吗?
这是我想要实现的示例:
电流:
<person uuid="6ec0cf24-e880-431b-ada0-a5835e2a565a">
<relation type="CHILD">
<to xsi:type="personRef" uuid="56a930c0-5499-467f-8263-c2a9f9ecc5a0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</relation>
<relation type="CHILD">
<to xsi:type="personRef" uuid="6ec0cf24-e880-431b-ada0-a5835e2a565a" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</relation>
<!-- SNIP: some more relations -->
</person>
通缉:
<person uuid="6ec0cf24-e880-431b-ada0-a5835e2a565a" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<relation type="CHILD">
<to xsi:type="personRef" uuid="56a930c0-5499-467f-8263-c2a9f9ecc5a0"/>
</relation>
<relation type="CHILD">
<to xsi:type="personRef" uuid="6ec0cf24-e880-431b-ada0-a5835e2a565a"/>
</relation>
<!-- SNIP: some more relations -->
</person>
最佳答案
不是那样很漂亮,但您可以向根元素添加一个空的 schemaLocation:
marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "");
关于java - JAXB:如何避免 xmlns:xsi 的重复命名空间定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7280271/