How to parse a soap response in java -
i want parse soap response (xml file) in java. example want extract value cv2 last operation in file (event.xml). used xpath in code have error compilation:
"exception in thread "main" java.lang.classcastexception: org.apache.xerces.dom.deferredtextimpl cannot cast javax.xml.soap.node"
if have ideas, please me :) please see below soap xml
<?xml version='1.0' encoding='utf-8'?> <soapenv:envelope xmlns:soapenv="..."> <soapenv:body> <ns1:commandresponsedata xmlns:ns1="..."> <ns1:commandresult> <ns1:transactionresult> <ns1:operationresult> <ns1:operation name="operationname" modifier="modify"> <ns1:parameterlist> <ns1:stringparameter name="n1">v1</ns1:stringparameter> <ns1:datetimeparameter name="d1">value</ns1:datetimeparameter> </ns1:parameterlist> </ns1:operation> <ns1:operation name="operationname" modifier="modify"> <ns1:parameterlist> <ns1:structparameter name="ratingoutput"> <ns1:listparameter name="c1"> <ns1:structelement> <ns1:stringparameter name="b1">bal1</ns1:stringparameter> <ns1:longparameter xsi:type="ns1:longparameter" name="cv1" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">80 </ns1:longparameter> <ns1:longparameter xsi:type="ns1:longparameter" name="nb1" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> 1920 </ns1:longparameter> </ns1:structelement> </ns1:listparameter> </ns1:structparameter> </ns1:parameterlist> </ns1:operation> <ns1:operation name="operationname" modifier="modify"> <ns1:parameterlist> <ns1:structparameter name="ratingoutput"> <ns1:listparameter name="c1"> <ns1:structelement> <ns1:stringparameter name="b2">bal2</ns1:stringparameter> <ns1:longparameter xsi:type="ns1:longparameter" name="cv2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance">60 </ns1:longparameter> <ns1:longparameter xsi:type="ns1:longparameter" name="nb2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> 1860 </ns1:longparameter> </ns1:structelement> </ns1:listparameter> </ns1:structparameter> </ns1:parameterlist> </ns1:operation> </ns1:operationresult> </ns1:transactionresult> </ns1:commandresult> </ns1:commandresponsedata> </soapenv:body> </soapenv:envelope>
... , code
import java.io.ioexception; import javax.xml.parsers.parserconfigurationexception; import javax.xml.xpath.xpathexpressionexception; import org.xml.sax.saxexception; import javax.xml.parsers.documentbuilder; import javax.xml.parsers.documentbuilderfactory; import javax.xml.soap.node; import javax.xml.xpath.xpath; import javax.xml.xpath.xpathconstants; import javax.xml.xpath.xpathfactory; import org.w3c.dom.document; public class parseevent { static final string file = "src/xml/event.xml"; public static void main(string[] args) throws parserconfigurationexception, saxexception, ioexception, xpathexpressionexception { documentbuilderfactory dbf = documentbuilderfactory.newinstance(); dbf.setnamespaceaware(false); documentbuilder db = dbf.newdocumentbuilder(); document doc = (document) db.parse(file); xpath xpath = xpathfactory.newinstance().newxpath(); node cv2 = (node) xpath.evaluate("//operation [last()]/parameterlist/structparameter[@name='ratingoutput']/listparameter[@name='c1']/structelement/longparameter[@name='cv2']/text()", doc.getdocumentelement(), xpathconstants.node); system.out.println("the charged value of test :" + cv2.getnodevalue()); } }
Comments
Post a Comment