SOAP API 在可选括号中响应 XML。如何迎合它?

Posted

技术标签:

【中文标题】SOAP API 在可选括号中响应 XML。如何迎合它?【英文标题】:SOAP API is responding XML in an Optional Bracket. How to cater it? 【发布时间】:2018-08-18 04:55:08 【问题描述】:

我的代码sn-p是

class ViewController: UIViewController, XMLParserDelegate 
var is_SoapMessage: String = "<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=\"https://www.w3schools.com/xml/\"><Celsius>55</Celsius></CelsiusToFahrenheit></soap:Body></soap:Envelope>"
override func viewDidLoad() 
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let is_URL: String = "https://www.w3schools.com/xml/tempconvert.asmx?op=CelsiusToFahrenheit"
    let lobj_Request = NSMutableURLRequest(url: NSURL(string: is_URL)! as URL)
    let session = URLSession.shared
    //let err: NSError?
    lobj_Request.httpMethod = "POST"
    lobj_Request.httpBody = is_SoapMessage.data(using: String.Encoding.utf8)
    lobj_Request.addValue("www.w3schools.com", forHTTPHeaderField: "Host")
    lobj_Request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    lobj_Request.addValue(String(is_SoapMessage.characters.count), forHTTPHeaderField: "Content-Length")
   lobj_Request.addValue("https://www.w3schools.com/xml/CelsiusToFahrenheit", forHTTPHeaderField: "SOAPAction")
    let task = session.dataTask(with: lobj_Request as URLRequest, completionHandler: data, response, error -> Void in
       // print("Response: \(response)")
        let strData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print(strData)
        if error != nil
        
            print("Error: ", error!)
        
        let xmlParser = XMLParser(data: data!)
        xmlParser.delegate = self
        //xmlParser.shouldProcessNamespaces = false
        //xmlParser.shouldReportNamespacePrefixes = false
        //xmlParser.shouldResolveExternalEntities = false
        let bool: Bool = xmlParser.parse()
        print(bool)
    )
    task.resume()

private func parser(parser: XMLParser, didStartElement: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [NSObject : AnyObject]) 
    // Can see elements in the soap response being printed.
    print("Helloa");

func parserDidStartDocument(_ parser: XMLParser) 
    print("Started")

func parserDidEndDocument(_ parser: XMLParser) 
    print("Ended")

func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) 
    print("error")

真正调用了所有解析器函数,但从未调用过 didStartElement。我得到的响应在 Optional Tag 中,而 XML Parser 没有得到任何元素。有什么猜测吗?See Image Output

【问题讨论】:

【参考方案1】:

更新到最新

func parser(_ parser: XMLParser, 
 didStartElement elementName: String, 
    namespaceURI: String?, 
   qualifiedName qName: String?, 
      attributes attributeDict: [String : String] = [:]) 

【讨论】:

以上是关于SOAP API 在可选括号中响应 XML。如何迎合它?的主要内容,如果未能解决你的问题,请参考以下文章

如何在php中解析soap xml响应并从字符串中获取信息

如何从 PHP 中的以下 SOAP XML 响应中获取 pinBlocked 标记

如何从 SOAP 响应 XML 创建 WCF 消息对象

带有 XML 响应和正文的 SOAP 请求 swift 3,Alamofire

通过 Postman 调用 National Rail SOAP XML API

如何解析响应 xml 字符串?