Java 解组soap类强制转换异常

Java 解组soap类强制转换异常,java,web-services,soap,jaxb,cxf,Java,Web Services,Soap,Jaxb,Cxf,我遵循预定义的WSDL(和xsd)来发送和接收web服务调用。发送web服务发送任何类型的对象。我可以很好地寄出去。当我收到回复时,我们会得到一个任何元素的列表。下面是我们使用的代码: List<Object> list = academicRecordBatch.getBatchContent().getAny(); if (list != null && list.size() > 0) {

我遵循预定义的WSDL(和xsd)来发送和接收web服务调用。发送web服务发送任何类型的对象。我可以很好地寄出去。当我收到回复时,我们会得到一个任何元素的列表。下面是我们使用的代码:

        List<Object> list = academicRecordBatch.getBatchContent().getAny();
        if (list != null && list.size() > 0) {
                    Log.debug("got : "+ list.get(0).getClass().getName());
                    K12StudentType k12StudentType = (K12StudentType) list.get(0); //error on this line
        }
List List=academicRecordBatch.getBatchContent().getAny();
if(list!=null&&list.size()>0){
Log.debug(“get:+list.get(0.getClass().getName());
K12StudentType K12StudentType=(K12StudentType)列表。获取(0);//此行出错
}
这会产生以下错误: [java]2011-05-10 09:52:53707调试[com.mycompany.is.Test]main(第42行):返回的对象:org.pesc.message.academicrecordbatch.v2\u 0。AcademicRecordBatch@483bead5 [java]java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.ElementNSImpl不能强制转换为org.pesc.sector.academicrecord.v1_4.K12StudentType

看起来返回的是ElementNSImpl的列表。如何从中提取K12StudentType对象?

如果您有任何建议,我将不胜感激。

ElementNSImpl
实现了
节点
接口,并解释了如何解组节点对象。但是我从未尝试过。

ElementNSImpl
实现了
节点
接口,这说明了如何解组节点对象。但是我从未尝试过。

您是否正在访问从Java客户端返回数据集的.NET Web服务? 无论如何,试试这个: 假设变量“o”显示为ElementsImpl对象。将其强制转换为org.w3c.dom.Node对象,然后使用dom方法导航返回的XML,该XML现在可通过节点对象使用

import org.w3c.dom.*; // Add this import.

Object o = objs.get(0); // the ElementNSImpl object. 
Node dataSetNode = (Node)o;

// Some more code for illustration..            
if (dataSetNode != null) {
Node tableNode = dataSetNode.getFirstChild(); 
if (tableNode != null) {
Node dataElementNode = tableNode.getFirstChild();
        while (dataElementNode != null) {
            String text = dataElementNode.getTextContent();
            String name = dataElementNode.getNodeName();
            System.out.format("%s: %s\n", name, text);
            dataElementNode = dataElementNode.getNextSibling();
    }                   
}

您正在访问从Java客户端返回数据集的.NET Web服务吗? 无论如何,试试这个: 假设变量“o”显示为ElementsImpl对象。将其强制转换为org.w3c.dom.Node对象,然后使用dom方法导航返回的XML,该XML现在可通过节点对象使用

import org.w3c.dom.*; // Add this import.

Object o = objs.get(0); // the ElementNSImpl object. 
Node dataSetNode = (Node)o;

// Some more code for illustration..            
if (dataSetNode != null) {
Node tableNode = dataSetNode.getFirstChild(); 
if (tableNode != null) {
Node dataElementNode = tableNode.getFirstChild();
        while (dataElementNode != null) {
            String text = dataElementNode.getTextContent();
            String name = dataElementNode.getNodeName();
            System.out.format("%s: %s\n", name, text);
            dataElementNode = dataElementNode.getNextSibling();
    }                   
}

JAXB为“batchContent”属性生成了一个catch all
getAny()
getter,因为它可能包含任何元素。这就是为什么在进行转换之前需要显式检查元素类型。在解组之前提供适当的XSD部分和XML:可能会有更多有用的注释/想法。JAXB为“batchContent”属性生成了一个catch all
getAny()
getter,因为它可能包含任何元素。这就是为什么在进行转换之前需要显式检查元素类型。在解组之前提供适当的XSD部分和XML:可能会有更多有用的注释/想法。