我有一个正在处理的Swift项目,该项目使用URLSession创建SOAP请求。我设置了一个使用W3学校温度转换器并返回的示例,就像我希望xml返回的那样,<Title>
... </Title>
。但是,当我在内部SOAP服务器上执行请求时,大多数(但不是全部)节点的形式为<Title>
... </Title>
我假设这是一个编码问题,但我似乎无法弄清楚为什么它会重新出现。
我的解析器代码在解析<
时可以在控制台中看到它,但它选择<
作为元素而不是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><NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="TABLE" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="TABLE">
<xs:complexType>
<xs:sequence>
<xs:element name="GPID" type="xs:long" />
<xs:element name="Title" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="100" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="CP_Desc" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="1073741823" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<TABLE>
<GPID>5188</GPID>
<Title>MY TITLE</Title>
在第一行的末尾,节点/元素NewDataSet是第一个缺少'<'和'>'的元素。它也是xml文档末尾具有该格式的最后一个Ending节点/元素(上面未显示)。 ExecuteQueryWithReturnResult是最后一个在开始节点/元素上具有正确的<>格式的节点,以及在第一个结束节点/元素上具有开始重新显示正确格式的第一个结束节点/元素。
任何帮助/见解/建议将不胜感激。 (我一直在搜索并搜索可能的答案/解决方法,但无济于事)