java - Get SoapBody Element value -
here response server, although can take mm7version
element value, cannot status
element value. returns null.
soapmeesage xml response:
<?xml version='1.0' encoding='utf-8'?> <soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <soap-env:header> <mm7:transactionid soap-env:mustunderstand="1" xmlns:mm7="http://www.3gpp.org/ftp/specs/archive/23_series/23.140/schema/rel-5-mm7-1-2">tid.bip_trasnid.20041103.135200.001</mm7:transactionid> </soap-env:header> <soap-env:body> <mm7version>5.6.0</mm7version> <status><statuscode>2602</statuscode><statustext>invalid status</statustext></status> <messageid></messageid> </soap-env:body> </soap-env:envelope>
java code:
soapmessage response = connection.call(message, endpoint); soapenvelope env= response.getsoappart().getenvelope(); soapbody sb = env.getbody(); name elname = env.createname("status"); //get child elements name xelement iterator = sb.getchildelements(elname); //get first matched child element. //we know there one. soapbodyelement sbe = (soapbodyelement) it.next(); //get value xelement string myvalue = sbe.getvalue(); system.out.println("myvalue: "+myvalue);
coded method inspiring http://www.coderanch.com/t/640002/web-services/java/error-parsing-soap-response-message
private static mmsresponse getmmsresponse(soapmessage response) throws soapexception { mmsresponse mmsresponse = new mmsresponse(); iterator itr=response.getsoapbody().getchildelements(); while (itr.hasnext()) { node node=(node)itr.next(); if (node.getnodetype()==node.element_node) { element ele=(element)node; switch (ele.getnodename()) { case "mm7version": mmsresponse.setmm7version(ele.gettextcontent()); break; case "messageid": mmsresponse.setmessageid(ele.gettextcontent()); break; case "status": nodelist statusnodelist = ele.getchildnodes(); status status = new status(); for(int i=0;i<statusnodelist.getlength();i++){ element statuselement = (element) statusnodelist.item(i); switch (statuselement.getnodename()) { case "statuscode": status.setstatuscode(ele.getchildnodes().item(i).gettextcontent()); break; case "statustext": status.setstatustext(ele.getchildnodes().item(i).gettextcontent()); break; default: break; } } mmsresponse.setstatus(status); break; default: break; } } else if (node.getnodetype()==node.text_node) { //do nothing here likely, response never has mixed content type //this reference } }
Comments
Post a Comment