Archive
Using SWS on iPhone/iPad – Part 2 Parsing the XML response
SAX vs. DOM
- A SAX parser is one where your code is notified as the parser walks through the XML tree, and you are responsible for keeping track of state and constructing any objects you might want to keep track of the data as the parser marches through.
- A DOM parser reads the entire document and builds up an in-memory representation that you can query for different elements. Often, you can even construct XPath queries to pull out particular pieces.
XML Parsers for the iPhone
- NSXMLParser is a SAX parser included by default with the iPhone SDK. It’s written in Objective-C and is quite straightforward to use, but perhaps not quite as easy as the DOM model. It is the fastest parser in terms of parsing xml from start to finish.
- libxml2 is an Open Source library that is included by default with the iPhone SDK. It is a C-based API, so is a bit more work to use than NSXML. The library supports both DOM and SAX processing. The libxml2 SAX processor is especially cool, as it has a unique feature of being able to parse the data as it’s being read. For example, you could be reading a large XML document from the network and displaying data that you’re reading for it to the user while you’re still downloading. Therefore, while this parser is slower than NSXMLParser from start to finish, the user experience is improved in the case of large xml documents.
which provide other DOM capabilities such as XPath functionality. If you get into parsing complex xml structures these may be a good consideration. For our purposes we will use NSXMLParser. I’ve found it to be the path of least resistance and it really wants to be friendly so we’ll play along.
The XML
<GetSiteListResponse xmlns=”http://centershift.com/”>
<GetSiteListResult>
<Facility>
<SiteName>string</SiteName>
<SiteId>string
<DistanceTo>string</DistanceTo>
</Facility>
<Facility>
<SiteName>string</SiteName>
<SiteId>string</SiteId>
<DistanceTo>string</DistanceTo>
</Facility>
</GetSiteListResult>
</GetSiteListResponse>
</soap:Body>
Received %d bytes of data\n”,[webData length]);
cancelButtonTitle:@”OK” otherButtonTitles:nil];
([elementName isEqualToString:@”Facility”])
([currentElement isEqualToString:@”SiteName”])
([currentElement isEqualToString:@”SiteId”])
([elementName isEqualToString:@”Facility”])
Using SWS web services on iPhone/iPad – Part 1 Calling the service
iPhone/iPad Apps
In today’s marketplace users are looking for applications that extend their functionality to their iPhone/iPad device. Building business applications on the iPhone/iPad that communicating with web services that provides that functionality in the cloud is a great way to embrace this build forward thinking revolution. However, consuming web services in an iPhone application is not for the faint-of-heart. Unlike other development tools (such as Microsoft Visual Studio), Xcode does not have built-in tools that make consuming web services easy. Everything must be done by hand and you need to know how to form the relevant XML (SOAP) messages to send to the web services and then parse the returning XML result.
Using SOAP 1.1
POST /iService/iService1.asmx HTTP/1.1 Host: host05slc.centershift.com Content-Type: text/xml; charset=utf-8 Content-Length: length SOAPAction: "http://centershift.com/GetSiteList" string</UserName> <Password>string <!--GetSiteList>
To get a request to work from within xCode/Objective C
That’s the essence to format the SOAP message and post the request. In Objective C you’ll need to add the following for the methods of the NS objects we use:
This is a pretty vanilla service to call but all basic nuts and bolts are all there to call a web service in Objective C. In part 2 we’ll look at what we need to do to parse the xml and do something useful with the data. Please feel free to comment or email me with any questions or suggestions you’d like to make in relation to this post.