我遇到了解组已根据模式验证过的XML的问题。
我向我提供了一个大型模式,我已经扩展了。以下是所提供内容的摘录:
(命名空间“原创”)
<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。
谢谢,
布赖恩
答案 0 :(得分:0)
创建JAXB unmarshaller时,需要将其传递给上下文;也就是说,你解组的结构包。我们使用的实用程序类使我们可以轻松地执行此操作:将XML字符串和包名称传递给它,然后返回一个Object。
但是,当解组跨越多个名称空间和包的结构时,您需要为引用的每个包提供包名,否则unmarshaller没有正确的上下文。我从来不知道你可以将多个包名称传递给JAXB上下文。
WRONG:
// Pass package name for the message only.
JAXBContext jc = JAXBContext.newInstance("com.mycompany.my.messages");
Unmarshaller u = jc.createUnmarshaller();
RIGHT:
// Pass package names for all XML structures present in the message.
JAXBContext jc = JAXBContext.newInstance("com.mycompany.my.types:com.mycompany.my.messages:com.othercompany.their.types");
Unmarshaller u = jc.createUnmarshaller();
也许这是一个愚蠢的错误,但我想我会分享以防万一。读一点on this site为我提供了这个想法。上面的词汇也提到了这个,但我以为他在谈论阶级问题;所有生成的类始终可用于上下文,但是没有告诉上下文查找它们。