【发布时间】:2011-06-15 22:57:56
【问题描述】:
我在解组已针对架构验证的 XML 时遇到问题。
我有一个提供给我的大型架构,我已经对其进行了扩展。以下是所提供内容的 sn-p:
(命名空间“原始”)
<xs:element name="Platform" type="PlatformType" abstract="true" />
<xs:complexType name="PlatformType" abstract="true">
...
</xs:complexType>
<xs:element name="SpringPlatform" type="SpringPlatformType" substitutionGroup="Platform" />
<xs:complexType name="SpringPlatformType">
<xs:complexContent>
<xs:extension base="PlatformType">
...
</xs:extension>
</xs:complexContent>
</xs:complexType>
现在我用一些额外的功能扩展了 SpringPlatformType:
<xsd:element name="MySpringPlatform" type="MySpringPlatformType"
substitutionGroup="original:SpringPlatform" />
<xsd:complexType name="MySpringPlatformType">
<xsd:complexContent>
<xsd:extension base="original:SpringPlatformType">
<xsd:sequence>
...
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
我收到一个平台列表,如下所示:
<xs:element name="PlatformList" type="PlatformListType" />
<xs:complexType name="PlatformListType">
<xs:sequence>
<xs:element ref="Platform" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
我可以正确解组使用原始的传入消息:SpringPlatform 元素,但是当我尝试解组我的派生/扩展类型 (MySpringPlatform) 时,我得到 null。 但是,XML 验证我的使用 XMLSpy 2011 的架构就好了。
有什么建议吗?我在 Java 6u24、Windows XP SP3 上使用 JAXB (Metro) 2.2.4。
谢谢,
布赖恩
【问题讨论】:
-
如何创建 JAXBContext?
-
我尝试了两种方法: 1) JAXBContext jc = JAXBContext.newInstance("package.name.here"); 2) JAXBContext jc = JAXBContext.newInstance(TypeName.class);
-
确保 JAXBContext 可以使用原始命名空间和扩展命名空间的包。好像你只有原始包。
-
我应该提一下,我在扩展命名空间中定义了其他对象(如 Header),这些对象(如 Header)解组就好了(即使在同一条消息中),所以我认为这不是可用包的问题.消息中的所有内容(还有很多其他内容)都将 except 解组为该替代组元素列表中的那些内容。
标签: xml xml-serialization xsd