Parse SOAP response using NSXMLParser from web service in Swift -
i'm passing soap message publicly available web service in swift. can see getting soap response , attempting parse using nsxmlparser.
i expecting make use of parser method has foundcharacters parameter - called once , contains string : "error"
i want able access result believe should contained within celsiustofahrenheitresult tag
the web service i'm calling , soap messages can found here.
i can see response contains following elements:
- soap:envelope
- soap:body
- celsiustofahrenheitresponse
- celsiustofahrenheitresult
but why isn't values held in these being printed?
a similar question can found here: parsing soap response using nsxmlparser swift lacks complete answer.
full code view controller calls web service on button press:
import uikit class viewcontroller: uiviewcontroller, uitextfielddelegate, nsurlconnectiondelegate, nsxmlparserdelegate { var mutabledata:nsmutabledata = nsmutabledata.alloc() @iboutlet weak var button: uibutton! @iboutlet weak var textfield: uitextfield! override func viewdidload() { super.viewdidload() // additional setup after loading view, typically nib. } // these counts own reference - checking these later. var didstartelement = 0 var didendelement = 0 var foundcharacters = 0 func callwebservice(){ // taken input. var celcius = "10"; var soapmessage = "<?xml version='1.0' encoding='utf-8'?><soap:envelope xmlns:xsi='http://www.w3.org/2001/xmlschema-instance' xmlns:xsd='http://www.w3.org/2001/xmlschema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'><soap:body><celsiustofahrenheit xmlns='http://www.w3schools.com/webservices/tempconvert.asmx'><celsius>\(celcius)</celsius></celsiustofahrenheit></soap:body></soap:envelope>" println(soapmessage); var urlstring = "http://www.w3schools.com/webservices/tempconvert.asmx?op=celsiustofahrenheit" var url = nsurl(string: urlstring) var therequest = nsmutableurlrequest(url: url!) var msglength = string(count(soapmessage)) therequest.addvalue("text/xml; charset=utf-8", forhttpheaderfield: "content-type") therequest.addvalue(msglength, forhttpheaderfield: "content-length") therequest.addvalue("http://www.w3schools.com/webservices/celsiustofahrenheit", forhttpheaderfield: "soapaction") therequest.httpmethod = "post" therequest.httpbody = soapmessage.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) var connection = nsurlconnection(request: therequest, delegate: self, startimmediately: true) // not call start since above call start connection } @ibaction func convertbuttonclicked(sender: anyobject) { callwebservice(); } override func didreceivememorywarning() { super.didreceivememorywarning() // dispose of resources can recreated. } func connection(connection: nsurlconnection!, didreceiveresponse response: nsurlresponse!) { println(response); if let httpresponse = response as? nshttpurlresponse { // printing status code here. println("error \(httpresponse.statuscode)") } // got response set length of data 0 since adding // if got data back. mutabledata.length = 0; } func connection(connection: nsurlconnection!, didreceivedata data: nsdata!) { mutabledata.appenddata(data) } // parse result right after loading func connectiondidfinishloading(connection: nsurlconnection!) { println(mutabledata) var xmlparser = nsxmlparser(data: mutabledata) xmlparser.delegate = self xmlparser.shouldprocessnamespaces = false xmlparser.shouldreportnamespaceprefixes = false xmlparser.shouldresolveexternalentities = false xmlparser.parse() } func parser(parser: nsxmlparser, didstartelement elementname: string, namespaceuri: string?, qualifiedname qname: string?, attributes attributedict: [nsobject : anyobject]) { didstartelement += 1 // can see elements in soap response being printed. println(elementname); } func parser(parser: nsxmlparser, didendelement elementname: string, namespaceuri: string?, qualifiedname qname: string?) { didendelement += 1 } func parser(parser: nsxmlparser, foundcharacters string: string?) { foundcharacters += 1 // prints "error" println("found chars: \(string!)"); } }
the problem not nsxmlparser
. have problem request soap message, make sure use correct namespace:
was:
<celsiustofahrenheit xmlns='http://www.w3schools.com/webservices/tempconvert.asmx'>
fixed:
<celsiustofahrenheit xmlns="http://www.w3schools.com/webservices/">
Comments
Post a Comment