无法使用 SWXMLHash 从 Webservice 解析 XML
Unable to parse XML from Webservice using SWXMLHash
我有以下使用 SWXMLHash 的代码 - XML 解析器似乎无法处理它。我检查了 URL 以确保它 returns 数据:
let baseUrl = "http://apps.hha.co.uk/mis/Api/getlivesensors.aspx?key=6fb21537-fb4e-4fe4-b07a-d8d68567c9d1"
var request = URLRequest(url: NSURL(string: baseUrl)! as URL)
let session = URLSession.shared
request.httpMethod = "GET"
//var err: NSError?
let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
if data == nil {
print("dataTaskWithRequest error: \(error)")
return
}
let xml = SWXMLHash.parse(data!)
if (xml["Sensors"]["Sensor"]["Name"].element?.text) != nil
//if (xml["Sensors"]["Sensor"]["Name"].element?.text) != nil
{
self.sensors.add(xml["Sensors"]["Sensor"]["Name"].element?.text as Any)
}
DispatchQueue.main.async(execute : {
print(self.sensors)
})
}
task.resume()
// but obviously don't try to use it here here
我从 url 得到的 XML 是这样的(标签被关闭 - 我只是没有包括它们):
<Sensors>
<Sensor>
<ID>12</ID>
<Name>EFM W.level</Name>
<Series>Level</Series>
<Unit>m</Unit>
<Values>
<Value CreatedOn="2017-01-08T13:50:00" Value="0.69"/>
<Value CreatedOn="2017-01-08T14:00:00" Value="0.72"/>
<Value CreatedOn="2017-01-08T14:10:00" Value="0.77"/>
<Value CreatedOn="2017-01-08T14:20:00" Value="0.82"/>
<Value CreatedOn="2017-01-08T14:30:00" Value="0.87"/>
有多个 Sensor
个元素,但只有一个 Sensors
个元素。
所以,而不是:
xml["Sensors"]["Sensor"]["Name"].element?.text
你应该有这样的东西:
xml["Sensors"]["Sensor"][0]["Name"].element?.text
这将从返回的组中获取第一个传感器。
要遍历传感器,您将得到类似的东西:
for sensor in xml["Sensors"]["Sensor"].all {
sessors.add(sensor["Name"].element?.text)
}
希望对您有所帮助!
好的。我已经解决了这个问题。问题出在我从服务器收到的 XML 文件上。看起来是UTF-8格式,但是文档上说是UTF-16。所以,我现在将接收到的数据对象转换为 UTF-8 NSString,然后将其转换为字符串!请看下面的代码:
let baseUrl = "http://apps.hha.co.uk/mis/Api/getlivesensors.aspx?key=6fb21537-fb4e-4fe4-b07a-d8d68567c9d1"
var request = URLRequest(url: NSURL(string: baseUrl)! as URL)
let session = URLSession.shared
request.httpMethod = "GET"
let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
if data == nil {
print("dataTaskWithRequest error: \(error)")
return
}
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)?.replacingOccurrences(of: "utf-16", with: "utf-8")
let xml = SWXMLHash.parse(dataString! as String)
//let xml = SWXMLHash.parse(dataString!)
if (xml["Sensors"]["Sensor"][0]["Name"].element?.text) != nil
{
self.sensors.add(xml["Sensors"]["Sensor"][0]["Name"].element?.text as Any)
}
DispatchQueue.main.async(execute : {
print(self.sensors)
})
}
task.resume()
// but obviously don't try to use it here here
我有以下使用 SWXMLHash 的代码 - XML 解析器似乎无法处理它。我检查了 URL 以确保它 returns 数据:
let baseUrl = "http://apps.hha.co.uk/mis/Api/getlivesensors.aspx?key=6fb21537-fb4e-4fe4-b07a-d8d68567c9d1"
var request = URLRequest(url: NSURL(string: baseUrl)! as URL)
let session = URLSession.shared
request.httpMethod = "GET"
//var err: NSError?
let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
if data == nil {
print("dataTaskWithRequest error: \(error)")
return
}
let xml = SWXMLHash.parse(data!)
if (xml["Sensors"]["Sensor"]["Name"].element?.text) != nil
//if (xml["Sensors"]["Sensor"]["Name"].element?.text) != nil
{
self.sensors.add(xml["Sensors"]["Sensor"]["Name"].element?.text as Any)
}
DispatchQueue.main.async(execute : {
print(self.sensors)
})
}
task.resume()
// but obviously don't try to use it here here
我从 url 得到的 XML 是这样的(标签被关闭 - 我只是没有包括它们):
<Sensors>
<Sensor>
<ID>12</ID>
<Name>EFM W.level</Name>
<Series>Level</Series>
<Unit>m</Unit>
<Values>
<Value CreatedOn="2017-01-08T13:50:00" Value="0.69"/>
<Value CreatedOn="2017-01-08T14:00:00" Value="0.72"/>
<Value CreatedOn="2017-01-08T14:10:00" Value="0.77"/>
<Value CreatedOn="2017-01-08T14:20:00" Value="0.82"/>
<Value CreatedOn="2017-01-08T14:30:00" Value="0.87"/>
有多个 Sensor
个元素,但只有一个 Sensors
个元素。
所以,而不是:
xml["Sensors"]["Sensor"]["Name"].element?.text
你应该有这样的东西:
xml["Sensors"]["Sensor"][0]["Name"].element?.text
这将从返回的组中获取第一个传感器。
要遍历传感器,您将得到类似的东西:
for sensor in xml["Sensors"]["Sensor"].all {
sessors.add(sensor["Name"].element?.text)
}
希望对您有所帮助!
好的。我已经解决了这个问题。问题出在我从服务器收到的 XML 文件上。看起来是UTF-8格式,但是文档上说是UTF-16。所以,我现在将接收到的数据对象转换为 UTF-8 NSString,然后将其转换为字符串!请看下面的代码:
let baseUrl = "http://apps.hha.co.uk/mis/Api/getlivesensors.aspx?key=6fb21537-fb4e-4fe4-b07a-d8d68567c9d1"
var request = URLRequest(url: NSURL(string: baseUrl)! as URL)
let session = URLSession.shared
request.httpMethod = "GET"
let task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
if data == nil {
print("dataTaskWithRequest error: \(error)")
return
}
let dataString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)?.replacingOccurrences(of: "utf-16", with: "utf-8")
let xml = SWXMLHash.parse(dataString! as String)
//let xml = SWXMLHash.parse(dataString!)
if (xml["Sensors"]["Sensor"][0]["Name"].element?.text) != nil
{
self.sensors.add(xml["Sensors"]["Sensor"][0]["Name"].element?.text as Any)
}
DispatchQueue.main.async(execute : {
print(self.sensors)
})
}
task.resume()
// but obviously don't try to use it here here