XML and the NSXMLParser class
To parse XML documents in Swift, we will use Apple's NSXMLParser
class. While there are several alternatives to NSXMLParser
, each with their own advantages and disadvantages, I have always found NSXMLParser
to be simple to understand and use. It is also designed in a way that is consistent with Apple's other APIs, which means if we are familiar with Apple's other APIs, NSXMLParser
will seem pretty straightforward.
The NSXMLParser
class is a Simple API for XML (SAX) parser. SAX parsers provide a mechanism to parse XML documents sequentially. Unlike
Document Object Model (DOM) parsers, which read the entire document into the memory and then parses it, a SAX parser reports on each parsing event as it happens and then discards most of them. This allows for a much smaller memory footprint while parsing. It also means that we need to have code to handle each parsing event that is needed to parse the XML document.
NSXMLParser
can parse XML documents from a URL, an NSData...