0

我有以下使用 SWXMLHash 的代码——但是 XML 解析器似乎无法处理它。我检查了 URL 以确保它返回数据:

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"/>

应用程序运行时的局部变量视图

4

2 回答 2

1

有多个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)
}

希望这可以帮助!

于 2017-01-10T02:53:08.480 回答
0

好的。我已经解决了这个问题。问题在于我从服务器收到的 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
于 2017-01-10T12:57:18.683 回答