【发布时间】:2015-09-29 20:35:55
【问题描述】:
我正在快速将 SOAP 消息传递给公开可用的 Web 服务。我可以看到我收到了一个 SOAP 响应,我正在尝试使用 NSXMLParser 解析它。
我期待使用具有 foundCharacters 参数的解析器方法 - 但这只调用一次并且只包含字符串:“Error”
我希望能够访问我认为应该包含在 CelsiusToFahrenheitResult 标记中的结果
我正在调用的 Web 服务和 SOAP 消息可以在 here. 找到
我可以看到响应包含以下元素:
- 肥皂:信封
- 肥皂:身体
- CelsiusToFahrenheitResponse
- CelsiusToFahrenheitResult
但是为什么这些中的值不被打印出来呢?
可以在此处找到类似的问题: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!)");
}
}
【问题讨论】:
标签: swift web-services soap