To parse XML documents in Swift, we will use Apple's NSXMLParser
class. While there are several alternatives to NSXMLParser
, each with its 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 parse it, a SAX parser reports on each parsing event as it happens. 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.
The NSXMLParser
class can parse XML documents from a URL, an NSData
object, or through...