java - Can't process SOAP response -


i have following code:

soapconnectionfactory sfc = soapconnectionfactory.newinstance();           soapconnection connection = sfc.createconnection();            messagefactory mf = messagefactory.newinstance();           soapmessage sm = mf.createmessage();           soapheader sh = sm.getsoapheader();           soapbody sb = sm.getsoapbody();           sh.detachnode();           mimeheaders mimeheader = sm.getmimeheaders();            //change header's attribute           mimeheader.setheader("soapaction", "\"urn:belkin:service:basicevent:1#getbinarystate\"");           //sh.addattribute(soapfactory.newinstance().createname("soapaction", "", "urn:belkin:service:basicevent:1#setbinarystate"),"");           qname bodyname = new qname("urn:belkin:service:basicevent:1", "getbinarystate", "u");            //sm.add                        soapbodyelement bodyelement = sb.addbodyelement(bodyname);           qname qn = new qname("binarystate");            system.out.println("\n soap request:\n");           sm.writeto(system.out);           system.out.println();            url endpoint = new url("http://" + ipaddress + ":49153/upnp/control/basicevent1");           soapmessage response = connection.call(sm, endpoint);            connection.close();           system.out.println(response.getcontentdescription());         } catch (exception ex) {           ex.printstacktrace();         } 

giving me response (in wireshark)

<s:envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"                         s:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/">     <s:body>         <u:getbinarystateresponse xmlns:u="urn:belkin:service:basicevent:1">             <binarystate>0</binarystate>         </u:getbinarystateresponse>     </s:body> </s:envelope> 

i want binarystate value (0) or (1) in java. i'm having hard time trying figure out located in soapmessage response. great. or if put xml response in string , parse myself.

to turn soapmessage string, have

soapmessage response = connection.call(sm, endpoint); 

do this:

soapmessage response = connection.call(sm, endpoint); bytearrayoutputstream os = new bytearrayoutputstream(); response.writeto(os); string responsexml = new string(os.tobytearray()); // work responsexml here... 

edit:

as alternative, if want specific value of element know name (and @ least 1 exist), may trough this:

string responsebinarystate = response.getsoapbody()                                      .getelementsbytagname("binarystate")                                      .item(0).gettextcontent(); system.out.println("binarystate: "+responsebinarystate); 

Comments