Search code examples
htmliosparsingswiftnsxmlparser

Swift parser.parse() returns false, but no parserError


I'm trying to create an iOS app that reads the RSS feed from a website (in my case http://www.quest.nl/rss), parses the RSS to a UITableView, from which the user can select an article. The selected article is then presented in an UIWebView. I can successfully parse the XML, but when I try to parse the article link (article link is html, but I figured i might as well use NSXMLParser too) I run into problems. parser?.parse() returns false (meaning something went wrong), but the contents of parser?.parserError are still nil.

My code:

@objc protocol HTMLParserDelegate{
    func parsingWasFinished()
}

import UIKit

class HTMLParser: NSObject, NSXMLParserDelegate {

    func startParsingWithContentsOfURL(linkURL: NSURL) {
        let parser = NSXMLParser(contentsOfURL: linkURL)
        parser?.delegate = self
        println(parser?.parse())
    }

    func parser(parser: NSXMLParser!, parseErrorOccured parseError: NSError!) {
        println(parseError.description)
    }

    func parser(parser: NSXMLParser, 
                validationErrorOccured validationError: NSError!) {
        println(validationError.description)
    }

    func parserDidEndDocument(parser: NSXMLParser!) {
        //println("finished! :D")
        delegate?.parsingWasFinished()
    }
}

I have tried deleting all methods I implemented for the parser (see above), but even that didn't solve the parsing.

Using a url for testing (http://www.quest.nl/foto/de-5-gekste-dieren-van-2014), I noticed that the parser gets to line 80, but doesn't get to the on line 95, while successfully parsing all previous tags.


Solution

  • Thanks to the comment by Rob I got to take a look at Hpple, which solved my problems.

    For anyone looking for an answer on my question, I'll shortly detail my steps. Indeed, HTML is not equal to XML, and even though the HTML I used was formatted quite nicely, XML Parsers had huge problems with dealing with the code. Then I turned to Hpple, which is extremely nice and user friendly, and used code I found at Github, which I added to my project, and from that point on, I could use Hpple and the nice, easy to use, syntax to parse the HTML, without any problems.

    Thanks everyone for the tips!