Unmarshalling soap class cast exception
I'm following a pre-defined WSDL (and xsd) to send and recive web service calls. The sending web service sends the 开发者_如何转开发object of type Any. I'm able to send it fine. When I receive the reply, we get a list of Any elements. Below is the code we use:
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
}
This produces the following error: [java] 2011-05-10 09:52:53,707 DEBUG [com.mycompany.is.Test] main(line 42): Object returned: org.pesc.message.academicrecordbatch.v2_0.AcademicRecordBatch@483bead5 [java] java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.ElementNSImpl cannot be cast to org.pesc.sector.academicrecord.v1_4.K12StudentType
Looks like what is returned is the list of ElementNSImpl. How can I extract my K12StudentType object from it?
I would appreciate any suggestion.Are you accessing a .NET webservice which returns a DataSet from a Java client? Anyway, try this: Let's say the variable 'o' is showing up as a ElementNSImpl object. Cast it to a org.w3c.dom.Node object and then use DOM methods to navigate the returned XML now made available via the Node object.
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();
}
}
ElementNSImpl
implements the Node
interface and this link explains how to unmarshal a node object. I've never tried it though.
精彩评论