首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Java - JAXB强制XML节点为ElementNSImpl数据类型

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

Stack Overflow用户
提问于 2018-09-27 16:45:03
回答 1查看 1.2K关注 0票数 0

我有以下QueryResult对象类:

代码语言:javascript
复制
@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;
}

我想解组后面的queryResultXml变量,它是一个jdom.Document:

代码语言:javascript
复制
<?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>

我使用以下代码来解组:

代码语言:javascript
复制
QueryResult result = XmlHelper.parseGenericObjectFromXmlString(new XMLOutputter().outputString(queryResultXml), QueryResult.class)

parseGenericObjectFromXmlString方法:

代码语言:javascript
复制
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抛出以下异常:

代码语言:javascript
复制
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属性设置为ElementNSImpl,但是为什么不将未编组的内容映射为元素数据类型呢?JAXB是否使用@XmlAnyElement注释对所有属性执行此操作?

EN

回答 1

Stack Overflow用户

发布于 2018-09-29 15:32:43

例外情况

代码语言:javascript
复制
java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to org.jdom.Element

代码语言:javascript
复制
@XmlAnyElement(lax=true)
protected Element content;

显然,您使用的是org.jdom.Element。但是相反,您需要使用org.w3c.dom.Element

用法:

@XmlAnyElement

@XmlAnyElement(lax="true")

public Object[] others;@XmlAnyElement

私有Element节点;

JAXB使用接口org.w3c.dom.Element的实现类ElementNSImpl

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52532916

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档