3

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Asif Raza
  • 836
  • 12
  • 29

2 Answers2

3

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)
        }
    }
sundance
  • 2,930
  • 1
  • 20
  • 25
  • This is correct - when Swift 4 came out, there were breaking changes with the Sequence protocol so it was removed in favor of calling the all method. – David Mohundro Jan 04 '18 at 12:44
  • I tried Alamofire.request("https://itunes.apple.com/us/rss/topgrossingapplications/limit=10/xml").response { response in debugPrint(response) guard let data = response.data else { return completion(.Failure("No weather item found")) } let xml = SWXMLHash.parse(data) let nodes = xml["feed"]["entry"] for node in nodes.all { print(node["title"].description) } } – Asif Raza Jan 05 '18 at 18:45
  • 1- po xml ▿ ▿ element : 2- po xml["feed"] ▿ ▿ xmlError : XML Element Error: Incorrect key ["feed"] - key : "feed" 3- po xml["feed"]["entry"] ▿ ▿ xmlError : XML Element Error: Incorrect key ["entry"] - key : "entry" – Asif Raza Jan 05 '18 at 18:45
  • Please extend your question. What you wrote in the comments is not readable. – sundance Jan 06 '18 at 11:54
1

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)
    }
}
sundance
  • 2,930
  • 1
  • 20
  • 25
  • I am getting some symbolic response when I am parsing my another xml after response from server like below:- XML: [Hindi] लखनऊ, वाराणसी, पटना, गया में सीज़न का सबसे कम न्यूनतम तापमान, दिन में मिलेगी राहत, रात में बढ़ेगी सर्दी 05 January, 2018 – Asif Raza Jan 05 '18 at 18:58
  • It has some other languages contents in hindi, tamil (like some indian languages) as mentioned in above comment. When I am trying to print it prints some symbolic characters like: - po entry.firstChild(tag: "title")?.stringValue ▿ Optional - some : "[Hindi] लà¤à¤¨à¤, वाराणसà¥, पà¤à¤¨à¤¾, à¤à¤¯à¤¾ मà¥à¤ सà¥à¥à¤¨ à¤à¤¾ सबसॠà¤à¤® नà¥à¤¯à¥à¤¨à¤¤à¤® तापमान, दिन मà¥à¤ मिलà¥à¤à¥ राहत, रात मà¥à¤ बà¥à¥à¤à¥ सरà¥à¤¦à¥" – Asif Raza Jan 05 '18 at 19:00