解组肥皂类强制转换异常

时间:2011-05-10 22:26:40

标签: java web-services soap jaxb cxf

我正在遵循预定义的WSDL(和xsd)来发送和接收Web服务调用。发送Web服务发送Any类型的对象。我能发好。当我收到回复时,我们会得到一个Any元素列表。以下是我们使用的代码:

        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
        }

这会产生以下错误:      [java] 2011-05-10 09:52:53,707 DEBUG [com.mycompany.is.Test] main(第42行):返回的对象:org.pesc.message.academicrecordbatch.v2_0.AcademicRecordBatch@483bead5      [java] java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.ElementNSImpl无法强制转换为org.pesc.sector.academicrecord.v1_4.K12StudentType

看起来返回的是ElementNSImpl列表。如何从中提取我的K12StudentType对象?
我很感激任何建议。

2 个答案:

答案 0 :(得分:3)

您是否正在访问从Java客户端返回DataSet的.NET Web服务? 无论如何,试试这个: 让我们说变量&#39; o&#39;显示为ElementNSImpl对象。将其转换为org.w3c.dom.Node对象,然后使用DOM方法导航现在通过Node对象提供的返回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();
    }                   
}

答案 1 :(得分:1)

ElementNSImpl实现了Node接口,此link解释了如何解组节点对象。我从来没有尝试过。

相关问题