Swift-解析SOAP(URLSession)XML响应-编码错误

时间:2018-08-09 04:39:58

标签: swift xml soap xml-parsing

我有一个正在处理的Swift项目,该项目使用URLSession创建SOAP请求。我设置了一个使用W3学校温度转换器并返回的示例,就像我希望xml返回的那样,<Title> ... </Title>。但是,当我在内部SOAP服务器上执行请求时,大多数(但不是全部)节点的形式为&lt;Title&gt; ... &lt;/Title&gt;

我假设这是一个编码问题,但我似乎无法弄清楚为什么它会重新出现。

我的解析器代码在解析<时可以在控制台中看到它,但它选择<作为元素而不是Title

这是示例代码...

class ViewController: UIViewController, UITextFieldDelegate, XMLParserDelegate, URLSessionDataDelegate, URLSessionDelegate {

var mutableData:Data = Data.init()
var currentElementName:String = ""
var returnInfo = [String]()
var returnInfoNode = [String]()

@IBAction func actionClick(_ sender: UIButton!) {

    let session = URLSession.shared // Load configuration into Session
    let applicationID = "1234567890"
    let query = "SELECT * FROM MY_TABLE;"
    let soapMessage = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://www.w3.org/2003/05/soap-envelope'><soap:Body><ExecuteQueryWithReturn xmlns='http://mySOAPsample.org/'><sQuery>\(query)</sQuery><sApplicationID>\(applicationID)</sApplicationID></ExecuteQueryWithReturn></soap:Body></soap:Envelope>"
    let urlString = "http://mySOAPsample.com"
    let myUrl = URL(string: urlString);
    var theRequest = URLRequest(url:myUrl!)
    let msgLength = String(soapMessage.count)

    theRequest.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
    theRequest.addValue(msgLength, forHTTPHeaderField: "Content-Length")
    theRequest.addValue("http://mySOAPsample.com", forHTTPHeaderField: "SOAPAction")

    theRequest.httpMethod = "POST"
    theRequest.httpBody = soapMessage.data(using: String.Encoding.utf8, allowLossyConversion: false)

    let task = session.dataTask(with: theRequest as URLRequest){
        data, response, error in

        if error != nil {
            guard error == nil else {
                print("error : \(String(describing: error))")
                return
            }}
        let strData = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print(strData!)
        let myparser = XMLParser(data: data!)
        myparser.delegate = self
        myparser.parse()
        myparser.shouldResolveExternalEntities = true
    }
    task.resume()
}

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

    print("DidStartElement -- \(currentElementName)")
    currentElementName = elementName as String
    returnInfo.append(currentElementName)
}

func parser(_ parser: XMLParser, foundCharacters string: String) {

    if currentElementName == "ExecuteQueryWithReturnResponse" {
        //print("worked")
        DispatchQueue.main.async {
            //do something
        }
    }
}

但是当我从请求中获得结果时,它们看起来就像是...

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ExecuteQueryWithReturnResponse xmlns="http://mySOAPsample.org/"><ExecuteQueryWithReturnResult>&lt;NewDataSet&gt;
      &lt;xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"&gt;
        &lt;xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="TABLE" msdata:UseCurrentLocale="true"&gt;
          &lt;xs:complexType&gt;
            &lt;xs:choice minOccurs="0" maxOccurs="unbounded"&gt;
              &lt;xs:element name="TABLE"&gt;
                &lt;xs:complexType&gt;
                  &lt;xs:sequence&gt;
                    &lt;xs:element name="GPID" type="xs:long" /&gt;
                    &lt;xs:element name="Title" minOccurs="0"&gt;
                      &lt;xs:simpleType&gt;
                        &lt;xs:restriction base="xs:string"&gt;
                          &lt;xs:maxLength value="100" /&gt;
                        &lt;/xs:restriction&gt;
                      &lt;/xs:simpleType&gt;
                    &lt;/xs:element&gt;
                    &lt;xs:element name="CP_Desc" minOccurs="0"&gt;
                      &lt;xs:simpleType&gt;
                        &lt;xs:restriction base="xs:string"&gt;
                          &lt;xs:maxLength value="1073741823" /&gt;
                        &lt;/xs:restriction&gt;
                      &lt;/xs:simpleType&gt;
                    &lt;/xs:element&gt;
                  &lt;/xs:sequence&gt;
                &lt;/xs:complexType&gt;
              &lt;/xs:element&gt;
            &lt;/xs:choice&gt;
          &lt;/xs:complexType&gt;
        &lt;/xs:element&gt;
      &lt;/xs:schema&gt;
      &lt;TABLE&gt;
        &lt;GPID&gt;5188&lt;/GPID&gt;
        &lt;Title&gt;MY TITLE&lt;/Title&gt;

在第一行的末尾,节点/元素NewDataSet是第一个缺少'<'和'>'的元素。它也是xml文档末尾具有该格式的最后一个Ending节点/元素(上面未显示)。 ExecuteQueryWithReturnResult是最后一个在开始节点/元素上具有正确的<>格式的节点,以及在第一个结束节点/元素上具有开始重新显示正确格式的第一个结束节点/元素。

任何帮助/见解/建议将不胜感激。 (我一直在搜索并搜索可能的答案/解决方法,但无济于事)

0 个答案:

没有答案