JAXB 可以处理多个“根"吗?元素? [英] Can JAXB handle multiple "root" elements?

查看:21
本文介绍了JAXB 可以处理多个“根"吗?元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下的架构...

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).该类将包含一个 create 方法,用于使用 @XmlElementDecl 注释的每个可能的根元素.有关更多详细信息和示例,请参阅我写的 文章.

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. See this article I wrote for more details and examples.

这篇关于JAXB 可以处理多个“根"吗?元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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