Java-JAXB强制XML节点转换为ElementNSImpl数据类型

时间:2018-09-27 08:45:03

标签: java jaxb deserialization unmarshalling

我有以下QueryResult对象类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="QueryResult", namespace = "")
public class QueryResult {

    public QueryResult()
    {
        this.attachments = new ArrayList<QueryAttachment>();
    }

    @XmlElement(name = "Error")
    protected QueryError error;

    @XmlAnyElement(lax=true)
    protected Element content;
}

我想解组以下是jdom.Document的queryResultXml变量:

<?xml version="1.0" encoding="UTF-8"?>
<QueryResult xmlns:func="urn:oio:ebst:diadem:functions" xmlns:meta="urn:oio:ebst:diadem:metadata:1" xmlns:er="urn:oio:ebst:diadem:Byggeskadefondendokument:1">
    <er:Byggeskadefondendokumenter meta:key="MetadatKey">
        <er:EftersynsrapportIndikator meta:key="MetadatKey/1">false</er:EftersynsrapportIndikator>
        <er:ByggeskadefondendokumentSamling meta:key="MetadatKey/2" />
    </er:Byggeskadefondendokumenter>
</QueryResult>

我正在使用以下代码来解组​​:

QueryResult result = XmlHelper.parseGenericObjectFromXmlString(new XMLOutputter().outputString(queryResultXml), QueryResult.class)

parseGenericObjectFromXmlString方法:

static <T> T parseGenericObjectFromXmlString(String xml, Class<T> genericType) {
        JAXBContext jc = JAXBContext.newInstance(genericType)
        Unmarshaller unmarshaller = jc.createUnmarshaller()

        def obj = (T) unmarshaller.unmarshal(new StringReader(xml))
        return obj
    }

JAXB然后引发以下异常:

java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to org.jdom.Element

at diadem.dirigent.plugin.integration.QueryResult$JaxbAccessorF_content.set(FieldAccessor_Ref.java:45)
at com.sun.xml.internal.bind.v2.runtime.reflect.Accessor.receive(Accessor.java:151)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:597)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:165)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
at diadem.base.plugin.helpers.XmlHelper.parseGenericObjectFromXmlString(XmlHelper.groovy:22)
at diadem.dirigent.plugin.IntegrationService.getResult(IntegrationService.groovy:95)
at diadem.dirigent.plugin.IntegrationService.callSourceSystem(IntegrationService.groovy:65)
at diadem.dirigent.plugin.IntegrationService.processQueryInput(IntegrationService.groovy:36)
at diadem.dirigent.plugin.IntegrationService.processQueryInput(IntegrationService.groovy:24)
at diadem.dirigent.plugin.IntegrationServiceSpec.test Integration processQuery with Byggeskadefond query definition(IntegrationServiceSpec.groovy:105)

JAXB自动将QueryResult.Content属性强制为Ele​​mentNSImpl,但是为什么它不将未编组的内容映射为Element数据类型呢? JAXB是否对带有@XmlAnyElement批注的所有属性执行此操作?

1 个答案:

答案 0 :(得分:0)

例外

java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to org.jdom.Element

告诉您您在错误的Element类中使用了

@XmlAnyElement(lax=true)
protected Element content;

很明显,您使用了org.jdom.Element。但是相反,您需要使用org.w3c.dom.Element

这在Javadoc of @XmlAnyElement用法示例中进行了描述。特别要注意那里给出的Element链接以及它们指向的位置。

  

用法:

     

@XmlAnyElement
  公开Element []其他;

     

// Element或JAXB元素的集合。
  @XmlAnyElement(lax =“ true”)
  public Object [] others;

     

@XmlAnyElement
   私有List <Element>节点;

     

@XmlAnyElement
  专用Element节点;

JAXB使用ElementNSImpl类,它是接口org.w3c.dom.Element的实现。