我有以下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;
}
我想取消编组以下查询结果XML变量,这是一个jdom。公文:
<?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 属性强制为 ElementNSImpl,但为什么它不将未编组的内容映射到 Element 数据类型呢?JAXB 是否对所有带有@XmlAnyElement注释的属性执行此操作?
例外
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 公共元素[] 其他;元素或 JAXB 元素的集合。
@XmlAnyElement(lax="true"(
public Object[] others;
@XmlAnyElement私有列表<元素>节点;元素>
@XmlAnyElement私有元素节点;
JAXB 使用类ElementNSImpl
,它是接口org.w3c.dom.Element
的实现。