• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
iOS: Web Services and XML parsing
 

iOS: Web Services and XML parsing

on

  • 5,860 views

 

Statistics

Views

Total Views
5,860
Views on SlideShare
5,859
Embed Views
1

Actions

Likes
2
Downloads
88
Comments
0

1 Embed 1

https://www.linkedin.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    iOS: Web Services and XML parsing iOS: Web Services and XML parsing Presentation Transcript

    • iOS:  Web  Services  and  XML  Parsing   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
    • About  Web  Services  •  Fetch  content  via  HTTP  •  Parse  the  content  •  Content  is  usually   –  XML   –  JSON  •  Example:  weather  data,  bus  traffic   informaLon,  twiMer…  
    • HTTP  GET  
    • Fetching  content  over  HTTP  •  In  iOS  it’s  easy  to  fetch  content  over  HTTP  •  Classes  needed   –  NSURL   –  NSURLRequest   –  NSURLConnecLon  •  The  NSURLConnecLon  has  a  delegate   NSURLConnecLonDelegate  which  holds   methods  like  didReceiveData  and   connecLonDidFinishLoading  
    • NSURLConnecLon  Example  
    • NSURLConnecLonDelegate  
    • PARSING  XML  
    • OpLons  •  SAX   –  Event  driven   –  Does  not  consume  lot  of  memory   –  iOS:  NSXMLParser    •  Dom   –  Reads  enLre  document  to  memory   –  You  can  move  in  the  object  tree   –  libxml2  –  C  based  API  for  SAX  and  DOM    
    • Third  Party  OpLons  •  TBXML   –  Lightweight  DOM,  read  only  •  TouchXML   –  DOM  and  XPath,  read  only  •  KissXML   –  TouchXML  +  ediLng  of  xml  •  GDataXML   –  DOM    and  Xpath,  read  and  write,  developed  by   Google  
    • NSData  vs.  NSMutableData  •  NSMutableData   –  Wrapper  for  byte  buffer.  Can  be  modified  •  NSData   –  Wrapper  for  byte  buffer.  Cannot  be  modified  •  In  didReceiveData  
    • Parsing  XML  Data  using  NSXMLParser  •  iOS  has  NSXMLParser  for  parsing  data  in  an   event  driven  way  (can  be  complicated).  •  The  NSXMLParser  opens  xml  data  and  calls   methods  like   –  didStartElement   –  foundCharacters   –  didEndElement  
    • How  to  start  parsing  XML?  The  NSXMLParserDelegate  provides  you  methods:  
    • JSON  
    • JSON  •  JavaScript  Object  NotaLon,  is  a  text-­‐based   open  standard  designed  for  human-­‐readable   data  interchange  •  RepresenLng  simple  data  structures  and   associaLve  arrays,  called  objects.  •  Despite  its  relaLonship  to  JavaScript,  it  is   language-­‐independent,  with  parsers  available   for  many  languages.  
    • Example  { "response":{ "version":"0.1", "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", "features":{ "conditions":1 } }, "current_observation":{ "image":{ "url":"http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png", "title":"Weather Underground", "link":"http://www.wunderground.com" }, "display_location":{ "full":"Tampere, Finland", "city":"Tampere", "state":"", "state_name":"Finland", "country":"FI", "country_iso3166":"FI", "zip":"00000", "latitude":"61.41999817", "longitude":"23.57999992",
    • JSON  Parsing  •  From  iOS  5  and  above,  you  can  do  JSON   parsing  using  NSJSONSerializaLon  •  Convert  JSON  to  foundaLon  objects  and  back  •  See  Doc   –  hMp://developer.apple.com/library/ios/ #documentaLon/FoundaLon/Reference/ NSJSONSerializaLon_Class/Reference/ Reference.html  
    • Parsing  NSError* errorObject;// _data (NSData) contains parsable json contentNSDictionary* json = [NSJSONSerialization JSONObjectWithData: _data options:NSJSONReadingMutableContainers error:&errorObject];NSDictionary* observation = [json objectForKey:@"current_observation"];NSDictionary* displayLocation = [observation objectForKey:@"display_location"];// “Tampere”NSString* city = [[NSString alloc] initWithFormat:@"%@", [displayLocation objectForKey:@"city"]]