Parsing an XML Response from the Cloud
Part of the iOS Cloud Development For Dummies Cheat Sheet
When you use a web service that's based in the cloud, your response might be in XML. If, when working with your cloud-based web service, you were to enter the following in your web browser
you'd get back some XML — this, for example:
<geometry> <location> <lat>40.7484395</lat> <lng>-73.9856709</lng> </location>
Since XML doesn't exactly express itself in ways that are easy to understand, you would then have to parse whatever it is that XML gives you.
Remember XML is made up of elements that have start and end tags. To get at the information through iCloud, you use the NSXMLParser class that uses the delegation pattern. Here's how that would work:
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:data]; [xmlParser setDelegate:self]; [xmlParser parse];
It turns out that there are four NSXMLParser delegate methods you will be implementing to parse an XML data structure:
parser:didStartElement:namespaceURI:qualifiedName:attributes: parser:foundCharacters: parser:didEndElement:namespaceURI:qualifiedName: parser:parseErrorOccurred:
parser:didStartElement:namespaceURI:qualifiedName:attributes: lets you know that the parser has found an element with a tag, <lat> for example, and it's up to you to extract the data in the parser:foundCharacters: method.