JAXB可以处理多个“root”吗?元素? [英] Can JAXB handle multiple "root" elements?
问题描述
我有一个类似于以下的架构......
I have a schema similar to the following...
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="t1" type="t1Type"/>
<xs:element name="t2" type="t2Type"/>
<xs:element name="t3" type="t3Type"/>
</xs:schema>
起初我认为这是一个无效的模式,但我在网上做的所有检查都验证了它。这意味着提供XML的人可以发送列出的任何(或所有)类型,并且仍然符合架构。
At first I thought this was an invalid schema but all the checks I do online validate it. This means the person supplying the XML can send any (or all) the types listed and still conform to the schema.
如何使用JAXB进行映射和解组所有不同的可能性?
How do I go about mapping and unmarshalling all the different possibilities using JAXB?
我不知道哪个其中我将收到。
I have no idea which of them I will be recieving.
推荐答案
您需要利用用 @XmlRegistry注释的工厂类
(通常称为 ObjectFactory
)。对于使用 @XmlElementDecl
注释的每个可能的根元素,该类将包含 create
方法。
You will need to leverage a factory class annotated with @XmlRegistry
(usually called ObjectFactory
). That class will contain a create
method for each possible root element annotated with @XmlElementDecl
.
示例
- http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html
这篇关于JAXB可以处理多个“root”吗?元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!