我将SOAP消息传递给swift中的公共Web服务。我可以看到我收到了SOAP响应,并且我试图使用NSXMLParser解析它。
我期望使用具有 foundCharacters 参数的解析器方法 - 但这只被调用一次并且只包含字符串:"错误"
我希望能够访问我认为应该包含在 CelsiusToFahrenheitResult 标记
中的结果我可以找到我正在调用的Web服务和SOAP消息here.
我可以看到响应包含以下元素:
但为什么这些价值也不会被印刷?
类似的问题可以在这里找到:Parsing SOAP response using NSXMLParser with Swift但是缺乏完整的答案。
我的视图控制器的完整代码,按下按钮调用Web服务:
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()
// Do any additional setup after loading the view, typically from a nib.
}
// These counts are just for my own reference - checking these later.
var didStartElement = 0
var didEndElement = 0
var foundCharacters = 0
func callWebService(){
// This would likely be taken from an 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)
// Do NOT call start since the above call will start the connection
}
@IBAction func convertButtonClicked(sender: AnyObject) {
callWebService();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func connection(connection: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
println(response);
if let httpResponse = response as? NSHTTPURLResponse {
// Just printing the status code here.
println("error \(httpResponse.statusCode)")
}
// We got a response to set the length of the data to zero since we will be adding to this
// if we actually got any data back.
mutableData.length = 0;
}
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
mutableData.appendData(data)
}
// Parse the 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 the 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
// This prints "Error"
println("found chars: \(string!)");
}
}
答案 0 :(得分:1)
问题不在于NSXMLParser
。您的请求soap消息有问题,请确保使用正确的命名空间:
<强> WAS:强>
<CelsiusToFahrenheit xmlns='http://www.w3schools.com/webservices/tempconvert.asmx'>
<强> FIXED:强>
<CelsiusToFahrenheit xmlns="http://www.w3schools.com/webservices/">