简体   繁体   English

使用带有Alamofire的SWXMLHash解析xml时出错

[英]Getting error in parsing xml using SWXMLHash with Alamofire

I am using pods with Swift 4 我正在使用带有Swift 4的pod

pod 'SWXMLHash', '~> 4.0.0'
pod 'Alamofire', '~> 4.5'

When I am parsing XML with below code, getting the error: 当我用下面的代码解析XML时,得到错误:

Type 'XMLIndexer' does not conform to protocol 'Sequence'

Code: 码:

Alamofire.request("https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml").response { response in
            debugPrint(response)

            guard let data = response.data else {
                return
            }

            let xml = SWXMLHash.parse(data)
            let nodes = xml["feed"]["entry"]
            for node in nodes {
                print(node["title"].text)
            }
        }

I am trying to access 'entry' tag list from above iTunes XML URL. 我试图从iTunes XML URL上面访问'entry'标签列表。 If there is any method to access and initialize the list of entries in class/struct, please help. 如果有任何方法可以访问和初始化类/结构中的条目列表,请帮助。

According to the documentation you need to add all : 根据文档,您需要添加all

Alamofire.request("https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml").response { response in
        debugPrint(response)

        guard let data = response.data else {
            return
        }

        let xml = SWXMLHash.parse(data)
        let nodes = xml["feed"]["entry"]
        for node in nodes.all {
            print(node["title"]?.text)
        }
    }

Another solution is to use Fuzi , which is based on the Ono framework, which had great support. 另一种解决方案是使用附子 ,这是基于小野框架,其中有很大的支持。

The following snippet will print the titles: 以下代码段将打印标题:

Alamofire.request("https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml").responseString { response in
    guard let xml = try? XMLDocument(string: response.value ?? "") else {
        return
    }
    guard let feed = xml.root else {
        return
    }
    for entry in feed.children(tag: "entry") {
        let title = entry.firstChild(tag: "title")?.stringValue ?? ""
        print(title)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM