简体   繁体   中英

Getting error in parsing xml using SWXMLHash with Alamofire

I am using pods with Swift 4

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

When I am parsing XML with below code, getting the error:

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. 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 :

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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