JAXB2 将 xsd:restrictions 映射为子类和(un)marshall xsi:types
我做了很多研究试图解决这个问题,但是我 仍然不成功。
我有许多遵循此架构的 XSD:
Simple_Identification.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="./Base_3039.xsd"/>
<xsd:include schemaLocation="./Simple_A.xsd"/>
<xsd:include schemaLocation="./Simple_S.xsd"/>
<xsd:include schemaLocation="./Simple_N.xsd"/>
<xsd:include schemaLocation="./Simple_V1.xsd"/>
<xsd:include schemaLocation="./Simple_L.xsd"/>
<xsd:include schemaLocation="./Simple_V.xsd"/>
<xsd:include schemaLocation="./Simple_C.xsd"/>
<xsd:simpleType name="Simple_Identification">
<xsd:restriction base="Base_3039"/>
</xsd:simpleType>
</xsd:schema>
,例如 Simple_S.xsd 如下所示:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="./Simple_Identification.xsd"/>
<xsd:simpleType name="Simple_S">
<xsd:restriction base="Simple_Identification">
<xsd:minLength value="14"/>
<xsd:maxLength value="14"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
最终,我希望能够生成 XML 文件包含 标签看起来像这样:
<Identification xsi:type="Simple_S">XYZUVW</Identification>
现在,在不启用mapSimpleTypeDef的情况下,我能够 marshall/unmarshall XML 文件,忽略 Simple_S 等简单类型。
启用mapSimpleTypeDef 类后,会生成简单的 类型。 Simple_Identification 映射到包含 Base_3039 字段的类。 Base_3039 类包含一个 String 字段。但是,Simple_Identifications 的不同子类型的类不扩展 Simple_Identification,而仅包含 Simple_Identification 类型的字段,这在编组/解组时没有帮助。
例如,当解组此 XML 文件时:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="...">
<Identification>EDS-200708021031-950012222329</Identification>
<Time>2007-08-02T10:31:44.449+01:00</Time>
<Function>9</Function>
<Sender>
<Identity xsi:type="Simple_S">111111380002111</Identity>
</Sender>
</Header>
Identity 的值被解组为 Simple_Identification 对象,而不是专门的 Simple_S 对象。此外,如果我编组回来,所有 xsi:type 属性都会在生成的 XML 中丢失。
所以,基本上,我的问题是如何正确解组并生成包含 xsi:types 的 XML。我正在使用的模式是否不适合执行此操作? xsd:restriction 是否不被 JAXB 解释为继承类型?
请注意,XSD 不是我可以修改的,我只需使用它们来正确读取和生成 XML。
感谢您花时间帮我解决这个问题!
-安卡
I have been doing a lot of research trying to figure this out, but am
still unsuccessful.
I have a number of XSD that follow this schema:
Simple_Identification.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="./Base_3039.xsd"/>
<xsd:include schemaLocation="./Simple_A.xsd"/>
<xsd:include schemaLocation="./Simple_S.xsd"/>
<xsd:include schemaLocation="./Simple_N.xsd"/>
<xsd:include schemaLocation="./Simple_V1.xsd"/>
<xsd:include schemaLocation="./Simple_L.xsd"/>
<xsd:include schemaLocation="./Simple_V.xsd"/>
<xsd:include schemaLocation="./Simple_C.xsd"/>
<xsd:simpleType name="Simple_Identification">
<xsd:restriction base="Base_3039"/>
</xsd:simpleType>
</xsd:schema>
where for example Simple_S.xsd looks like this:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:include schemaLocation="./Simple_Identification.xsd"/>
<xsd:simpleType name="Simple_S">
<xsd:restriction base="Simple_Identification">
<xsd:minLength value="14"/>
<xsd:maxLength value="14"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
Eventually, I would like to be able to generate XML files that contain
tags looking like this:
<Identification xsi:type="Simple_S">XYZUVW</Identification>
For now, without enabling mapSimpleTypeDef, I am able to
marshall/unmarshall XML files, ignoring the simple types like Simple_S.
After enabling mapSimpleTypeDef classes are generated for the simple
types. Simple_Identification is mapped to a class containing a Base_3039 field. And Base_3039 class contains a String field. However, the classes for the different subtypes of Simple_Identifications do not extend Simple_Identification but only contain a field of type Simple_Identification, which does not help when marshalling/unmarshalling.
For example, when unmarshalling this XML file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Header xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="...">
<Identification>EDS-200708021031-950012222329</Identification>
<Time>2007-08-02T10:31:44.449+01:00</Time>
<Function>9</Function>
<Sender>
<Identity xsi:type="Simple_S">111111380002111</Identity>
</Sender>
</Header>
the value of Identity is unmashalled to a Simple_Identification object not to a specialized Simple_S object. Moreover, if I marshall back all xsi:type attributes are lost in the generated XML.
So, basically, my question is how can I properly unmarshall and generate XMLs containing xsi:types. Are the schemas I'm working with not appropriate for doing this? Is xsd:restriction not interpreted as a type of inheritance by JAXB?
Note that the XSDs are not mine to modify, I just have to work with them to properly read and generate XMLs.
Thanks for taking the time to help me figure this out!
-Anca
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JAXB 确实支持 xsi:type,但在处理复杂类型(而不是简单类型)时,您只会看到生成的不同子类(以及创建的实例对象)。
仅使用构面限制简单类型不会导致使用 JAXB 创建新类。这是因为,纯粹从面向对象的角度来看,派生类无论如何看起来都与超类完全相同。
JAXB does support
xsi:type
, but you will only see different sub-classes generated (and instance objects created) when dealing with complex types, not simple types.Restricting a simple type using only a facet does not lead to the creation of a new class with JAXB. That is because, purely in object-oriented terms, the derived class would look exactly the same as the super-class anyway.