O'Reilly logo

Beginning iOS 5 Application Development by Wei-Meng Lee

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

PARSING THE XML RESPONSE

In the iOS SDK, you can use the NSXMLParser object to parse an XML response returned by the Web service. The NSXMLParser class is an implementation of the Simple API for the XML (SAX) mechanism, which parses an XML document serially.

An NSXMLParser object reads an XML document, scanning it from beginning to end. As it encounters the various items in the document (such as elements, attributes, comments, and so on), it notifies its delegates so that appropriate actions can be taken (such as extracting the value of an element, etc.).

In the following Try It Out, you will parse the XML result returned by the Web service so that you can obtain the exchange rate of the two currencies you sent to the Web service.

TRY IT OUT: Parsing the XML Result Returned by the Web Service

  1. Using the WebServices project created in the previous section, add the following statements to the WebServicesViewController.h file to parse the response from the Web service:
    #import <UIKit/UIKit.h>
    
    @interface WebServicesViewController : UIViewController
    <NSXMLParserDelegate, NSURLConnectionDelegate>
    {
         IBOutlet UITextField *txtAmount;
         NSMutableData *webData;
         NSString *matchingElement;
         NSURLConnection *conn;
         NSMutableString *soapResults;
         NSXMLParser *xmlParser;
         BOOL elementFound;
    }
    
    @property (nonatomic, retain) UITextField *txtAmount;
    
    - (IBAction)buttonClicked:(id)sender;
    
    @end
  2. In the WebServicesViewController.m file, add the following bold statements to the connectionDidFinishLoading: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required