Pages

Labels

Sunday, May 13, 2012

XML web Services in Iphone


         Web services are used to connect the database server to store and retrieve data. Web services are the bridge between view part and database. In iPhone, we have to handle the web services to parse the data. In the case of xml web service, Xcode itself provide an xml parser to parse the data from web services.
         To use the parser delegates, we have to add NSXmlParserDelegate in the header section. First we have to send a soap request to get the XmlData. After getting the XmlData, we have to parse it locally.

8.2  Send Soap Request and get XmlData



          We shall see an example for web service implementation. Starts with Xcode and create one view based application. Come to header file, add xmlParserDelegate , action and Declare  object for NSMutableData in the header section.

         After the declarations, header file will be like as follows,




        
         Come to implementation section and define the action invokeWebService. In invoke web service action, we have to implement the soap call. In this application, we are using one free web service to get the name of all countries in the world.

         We will get the web data as a bulk of countries and we have to parse that web data and store the list of countries in an array. Web service  soap message is available in following link,         

         Now the we shall see the implementation coding for invokeWebService action,












         We should set soap request as a string namely “SoapMessage” and set up an url with the http link. After that, we have to set URLRequest by adding header , content length , etc.

         After send the soap request, we have to initialize and set up the connection to get the web data.


 








        
         In connection finish loading function, we shall print the web data result to see whether the process is done properly . Web data will be of encoding type, first we shall convert that to a string by  applying UTF8Encoding scheme. The  result will be printed as follows,


        
8.3  Parsing and Store the XML data using XMLParser delegates.

         Now, we shall see the xml parsing process for the xml data which get through previous process. In the connection finish loading method, initiate the xmlParsing with xml data by adding following codings as follows,

    if (xmlParser)
    {
        [xmlParser release];
    }
    xmlParser = [[NSXMLParser alloc] initWithData: webdata];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:YES];
    [xmlParser parse];

         The above coding will initiate the parsing delegate functions , We have to write codes inside the delegate functions to parse and store the data in array. Following are the list of available XMLParser delegate functions,




          We can use the delegate functions as per our requirements, mainly we have to use three delegate functions to parse the data as follows,





        
         We have to declare a string called “parselemntname”  and a mutable array called
countryList in the header section and initialize the array in viewDidLoad function. Parse Element name  string will contains the current element name and by using the element name, we have to append data in the array in foundCharacter method.

         To print the array, use NSLog in didEndElement method. Finally we will get the array as follows,




         We can show the results using a table view or picker in iPhone.





8.4  UIActivityIndicator in iPhone

         Dealing with web service is a complex process. Some times it may took times to load the data from the server. To indicate that the process is still running, we have to use activity indicator view in our application.

         Add UIActivityIndicatorView object in the header section. In the implementation section, add two lines coding in invoke function as follows,




         here, av means activity indicator view object. In the connection finish loading method stop the animating and hide the activity view. Finally connect the outlets and actions , build and run the application, we will get the output as follows,





        





         8.5 Displaying data using TableView

         We get country list in array, now we have to display data using table view. For that purpose we have to Declare UITableView datasource and delegate in the header file. The header file will be look as follows,





         Now come to implementation section. In connection finish loading function , after the parsing code, reload the table using [tv reloadData] and set data source as follows,




         Now connect the actions and outlets in the xib file. Also connect datasource and delegate function of table view in to file's owner. We will get the output as follows,













9 comments:

ghousepasha said...

hello imay,

Very nice article with iphone. am a newbie to iphone development can u send me source code for this artcle on my email id mca.ghouse1@gmail.com

and do u have any example to submit data to webservice and return success or failure result on iphone.

Thanks once again for your effort

Unknown said...

hi,
It's great post, i got the webservice data in NSLog but not able to view the code to parse the XML and show the result in tableview. can you please send the example code to me on rajkumaryadav70@gmail.com

Thanks

Jawith Hussain Blog said...

hello friend .. ur coding soo nice..
can u please send me the code...
to mail id : rahjawith@gmail.com

Unknown said...

Hi Friend,

It's great post, i am new of the iphone application development i need to access the data. i am not able to view the code to parse the XML and show the result in tableview. can you please send the example code to me on lakshmipathi.kv001@gmail.com

Unknown said...

hi Friend


This is very nice one
can u send sample code....
annaduraisabapathy@gmail.com

Unknown said...

Hello friend
this is great post for webservice
can u please send me your source code on my email id
surendra2012@hotmail.com

falahudheen bin abdul salam said...

please send me full set of this project source code, as a fresher i cannot use this code.
mail id: falu238@gmail.com

Unknown said...

Code:

https://github.com/imayaselvan/Singleton-XML-parser

Aanoj said...

I really enjoy the blog.Much thanks again. Really Great.

iPhone Service Center in Chennai

Post a Comment

 
Loading