简体   繁体   English

JAXB没有将xml的任何元素解组为JAXBElement

[英]JAXB not unmarshalling xml any element to JAXBElement

I have a webservice call. 我有一个网络服务电话。 In my response when I try to get the xml any element in to a JAXBElement it throws an error. 在我的响应中,当我尝试将xml中的any元素放入JAXBElement时,它会抛出错误。

In the schema I have: 在模式中我有:

<xs:complexType name="InputType">
    <xs:annotation></xs:annotation>
    <xs:sequence>           
        <xs:element name="Id" type="xs:string" />
        <xs:any namespace="##any" processContents="lax" minOccurs="0" />
    </xs:sequence>
</xs:complexType>

The code I am using: 我正在使用的代码:

Object obj = inputType.getAny();
Object o = ((JAXBElement)obj).getValue(); 

This line throws the error: org.apache.xerces.dom.ElementNSImpl incompatible with javax.xml.bind.JAXBElement error in soap ui. 此行抛出错误: org.apache.xerces.dom.ElementNSImpl incompatible with javax.xml.bind.JAXBElement soap ui中的org.apache.xerces.dom.ElementNSImpl incompatible with javax.xml.bind.JAXBElement错误org.apache.xerces.dom.ElementNSImpl incompatible with javax.xml.bind.JAXBElement

Why doesn't it covert to JAXBElement? 为什么它不转换为JAXBElement? How do I make it work? 我如何使其工作?

If the property is annotated with the following the contents will be mapped as DOM nodes: 如果使用以下内容对属性进行批注,则内容将映射为DOM节点:

@XmlAnyElement

If the lax=true flag is set then known elements will be converted to domain objects: 如果设置了lax = true标志,则已知元素将转换为域对象:

@XmlAnyElement(lax=true)

For more information on @XmlAnyElement see: 有关@XmlAnyElement的更多信息,请参阅:


UPDATE #1 更新#1

With lax=true you can get a mix of domain objects and DOM nodes. 使用lax = true,您可以获得域对象和DOM节点的混合。 The following is from the java docs: 以下是来自java文档:

When true 当真的

If true, when an element matches a property marked with XmlAnyElement is known to JAXBContext (for example, there's a class with XmlRootElement that has the same tag name, or there's XmlElementDecl that has the same tag name), the unmarshaller will eagerly unmarshal this element to the JAXB object, instead of unmarshalling it to DOM. 如果为true,当一个元素与标记为XmlAnyElement的属性匹配时,JAXBContext就知道了(例如,有一个具有相同标记名称的XmlRootElement的类,或者具有相同标记名称的XmlElementDecl),unmarshaller将急切地解组此元素到JAXB对象,而不是将其解组为DOM。 Additionally, if the element is unknown but it has a known xsi:type, the unmarshaller eagerly unmarshals the element to a JAXBElement, with the unknown element name and the JAXBElement value is set to an instance of the JAXB mapping of the known xsi:type. 此外,如果元素未知但它具有已知的xsi:类型,则unmarshaller急切地将元素解组为JAXBElement,其中未知元素名称和JAXBElement值设置为已知xsi:类型的JAXB映射的实例。

As a result, after the unmarshalling, the property can become heterogeneous; 结果,在解组后,财产可能变得异质; it can have both DOM nodes and some JAXB objects at the same time. 它可以同时拥有DOM节点和一些JAXB对象。


UPDATE #2 更新#2

To ultimately solve the problem: 最终解决问题:

  1. Since it is possible for that property to contain a DOM node, your code should account for this possibility by doing some type checking. 由于该属性可能包含DOM节点,因此您的代码应通过执行某些类型检查来解释这种可能性。
  2. To reduce the amount of DOM nodes received you need to associate the possible root elements of those fragments with Java classes. 要减少接收的DOM节点数量,您需要将这些片段的可能根元素与Java类相关联。 This is done by annotating classes with @XmlRootElement(name="foo", namespace="bar"), or with @XmlElementDecl. 这是通过使用@XmlRootElement(name =“foo”,namespace =“bar”)或@XmlElementDecl注释类来完成的。

Check out my blog for an example: 查看我的博客中的示例:

validate your xml against your schema. 根据您的架构验证您的xml。 that should be the first thing to be checked 这应该是第一件要检查的事情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM