我有以下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;
}
我想解组后面的queryResultXml变量,它是一个jdom.Document:
<?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,但是为什么不将未编组的内容映射为元素数据类型呢?JAXB是否使用@XmlAnyElement注释对所有属性执行此操作?
发布于 2018-09-29 15:32:43
例外情况
java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to org.jdom.Element
@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
。
https://stackoverflow.com/questions/52532916
复制相似问题