iosswiftxmlparsing

Unable to parse XML from URL for indeed API


Well, I am using this library to parse XML: SwiftyXMLParser but unfortunately unable to get results array from the respond in iOS but I can see them on browser see this link or:

sample of respond!

This is my operation:

URLSession.shared.dataTask(with: NSURL(string: urlString)! as URL, completionHandler: { (data, response, error) -> Void in

    if error != nil {
        print(error)
        return
    }
    if let data = data {
        let xml = XML.parse(data)
        print(xml)
        //tried xml["results"]["0"] also didn't work
    }

}).resume()

This is my output in IOS :

<response version="2">
<query>ios</query>
<location>austin, tx</location>
<clickedCategories/>
<paginationPayload/>
<radius>25</radius>
<dupefilter>true</dupefilter>
<highlight>false</highlight>
<start>1</start>
<end>10</end>
<pageNumber>0</pageNumber>
<totalresults>315</totalresults>
<results>
//This is array but No results!!
<\results>

So does anyone knows why and how?

My attempt with JSON:

let indeedAPI = "api.indeed.com/ads/apisearch?publisher=4935138002921571&q=\(searchTerm)&format=json&l=%2C+tx&sort=&radius=50&st=&jt=&start=&limit=10&fromage=&filter=&latlong=1&co=&chnl=&userip=1.2.3.4&v=2"

        let googleYoutubeAPIUrl = URL(string: indeedAPI.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!)
        
        let request:URLRequest = URLRequest(url: googleYoutubeAPIUrl!, cachePolicy: NSURLRequest.CachePolicy.reloadIgnoringCacheData, timeoutInterval: 5.0)
        
        OperationQueue.main.cancelAllOperations()
        let task = URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
            
            if let _ = error {
                print(error.debugDescription)
                return
            }
            
            if let data = data {
                completion(JSON(data: data))
            } else { print("data nil"); return }
            
        })
        task.resume()

    {
      "paginationPayload" : "",
      "location" : "%2C tx",
      "dupefilter" : true,
      "results" : [
    // also no results
      ],
      "totalResults" : 0,
      "version" : 2,
      "end" : 0,
      "pageNumber" : 0,
      "start" : 0,
      "query" : "ios developer",
      "highlight" : true
    }

Solution

  • If you're looking for the result entries, you can do something like:

    let xml = XML.parse(data)
    
    guard xml.error == nil else {
        print("\(xml.error!)")
        return
    }
    
    let results = xml["response", "results"]
    
    guard results.error == nil && results["result"].error == nil else {
        print("no results")
        return
    }
    
    for result in results["result"] {
        if let jobtitle = result["jobtitle"].text {
            print("\(jobtitle)")
        }
    }