JAXB 创建空对象
我有一个基于 XSD 的 XML。 xsd
<xs:complexType name="objectOneType" mixed="true">
<xs:sequence>
<xs:element ref="nestedObject" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="attribute" type="xs:string" use="optional"/>
</xs:complexType>
现在,当我收到此 XML 时
<xmlresponse>
<xmlrequest>
<objectone attribute="changed" />
</xmlrequest>
<xmlsrc>
<objectone attribute="changed" />
</xmlsrc>
</xmlresponse>
,在此 XML 中,对象 OBjECTONE 没有名为 NESTEDOBJECT 的对象。现在,它不再忽略它,而是在 OBJECTONE 中插入一个 STRING 类型的空对象。因此,当我的代码尝试处理 OBJECTONE 时,假设它具有对象类型 NESTEDOBJECT。它抛出类广播异常。
有什么想法吗?
PS:杀了我,因为我正在使用 JAXB 1.2
I have an XML based on XSD. The xsd is
<xs:complexType name="objectOneType" mixed="true">
<xs:sequence>
<xs:element ref="nestedObject" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
<xs:attribute name="attribute" type="xs:string" use="optional"/>
</xs:complexType>
Now, when i receive this XML
<xmlresponse>
<xmlrequest>
<objectone attribute="changed" />
</xmlrequest>
<xmlsrc>
<objectone attribute="changed" />
</xmlsrc>
</xmlresponse>
In this XML, the object OBjECTONE doesn't have an object named NESTEDOBJECT. now instead of ignoring it, it is inserting a an empty object in OBJECTONE which is of STRING type. so when my code tries to process OBJECTONE assuming it has an object type NESTEDOBJECT. It throws classcast exception.
Any idea?
P.S: Kill me sicne i am using JAXB 1.2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关键是注释
mixed="true"
。有了这个注释结构,这样的结构就成为可能。 JAXB 在内部创建一个
List
This 是一个test
不应有前导或尾随String
mixed="true"
>List
The key is the annotation
mixed="true"
. With this annotation structures likeare possible. Internally JAXB creates a
List<Object> insideContent
which would be in the upper case classes of:String
,Italic
,String
,Underline
andString
. I assume that you output your XML with something likeMarshaller.JAXB_FORMATTED_OUTPUT
which inserts a newline and tabulator for the formatted output. You can either:<content><italic>This</italic> is a <underline>test</underline></content>
should not have a leading or trailingString
mixed="true"
instanceOf
while iterating theList<Object>