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
- 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
- In the WebServicesViewController.m file, add the following bold statements to the connectionDidFinishLoading: ...
Get Beginning iOS 5 Application Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.