Graphics Programs Reference
In-Depth Information
For the More Curious: NSXMLParser
NSXMLParser is the built-in XML parser in the iOS SDK. While there are plenty of pars-
ers you can pick up on the Internet, adding a third party dependency is sometimes difficult.
Many developers, seeing that NSXMLParser is not a tree-based parser (it doesn't create
an object graph out of the box), go searching for an alternative parser. However, in this
chapter, you've learned how to make NSXMLParser into a tree-based parser.
To parse simple XML, all you need are the three delegate methods used in this chapter.
More complex XML has element attributes, namespaces, CDATA, and a slew of other
items that need to be handled. NSXMLParser can handle these, too. The
NSXMLParserDelegate protocol includes many more methods that handle nearly any-
thing XML can throw at you. There are also arguments to the methods you have already
used that can handle more complex XML. For example, in pars-
er:didStartElement:namespaceURI:qualifiedName:attributes: , we
only used the first two arguments. For the other arguments, consider the following XML:
<?xml version="1.0" encoding="utf-8"?>
<container version="2.0" xmlns:foo="BNR">
<foo:item attribute1="one" attribute2="two"></item>
</container>
When the foo:item element is encountered by the parser, the values for the parameters
to the delegate method are as follows:
• The element is “item.” The namespace is ignored, and the name of the element
is kept.
• The namespaceURI is “BNR.” The element's name is item , and it is in the
foo namespace, which has a value of “BNR.”
• The qualifiedName is “foo:item.”
attributes is a dictionary that contains two keys, “attribute1” and “attribute2.”
Their values are “one” and “two,” respectively.
Search WWH ::




Custom Search