Your SlideShare is downloading. ×
0
iOS:	  Web	  Services	  and	  XML	  Parsing	                   Jussi	  Pohjolainen	       Tampere	  University	  of	  Appl...
About	  Web	  Services	  •  Fetch	  content	  via	  HTTP	  •  Parse	  the	  content	  •  Content	  is	  usually	      –  X...
HTTP	  GET	  
Fetching	  content	  over	  HTTP	  •  In	  iOS	  it’s	  easy	  to	  fetch	  content	  over	  HTTP	  •  Classes	  needed	  ...
NSURLConnecLon	  Example	  
NSURLConnecLonDelegate	  
PARSING	  XML	  
OpLons	  •  SAX	         –  Event	  driven	         –  Does	  not	  consume	  lot	  of	  memory	         –  iOS:	  NSXMLPa...
Third	  Party	  OpLons	  •  TBXML	     –  Lightweight	  DOM,	  read	  only	  •  TouchXML	     –  DOM	  and	  XPath,	  read...
NSData	  vs.	  NSMutableData	  •  NSMutableData	     –  Wrapper	  for	  byte	  buffer.	  Can	  be	  modified	  •  NSData	   ...
Parsing	  XML	  Data	  using	  NSXMLParser	  •  iOS	  has	  NSXMLParser	  for	  parsing	  data	  in	  an	     event	  driv...
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	...
Example	  {    "response":{       "version":"0.1",       "termsofService":"http://www.wunderground.com/weather/api/d/terms...
JSON	  Parsing	  •  From	  iOS	  5	  and	  above,	  you	  can	  do	  JSON	     parsing	  using	  NSJSONSerializaLon	  •  C...
Parsing	  NSError* errorObject;// _data (NSData) contains parsable json contentNSDictionary* json =      [NSJSONSerializat...
Upcoming SlideShare
Loading in...5
×

iOS: Web Services and XML parsing

7,261

Published on

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
7,261
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
150
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Transcript of "iOS: Web Services and XML parsing"

  1. 1. iOS:  Web  Services  and  XML  Parsing   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  2. 2. About  Web  Services  •  Fetch  content  via  HTTP  •  Parse  the  content  •  Content  is  usually   –  XML   –  JSON  •  Example:  weather  data,  bus  traffic   informaLon,  twiMer…  
  3. 3. HTTP  GET  
  4. 4. 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  
  5. 5. NSURLConnecLon  Example  
  6. 6. NSURLConnecLonDelegate  
  7. 7. PARSING  XML  
  8. 8. 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    
  9. 9. 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  
  10. 10. NSData  vs.  NSMutableData  •  NSMutableData   –  Wrapper  for  byte  buffer.  Can  be  modified  •  NSData   –  Wrapper  for  byte  buffer.  Cannot  be  modified  •  In  didReceiveData  
  11. 11. 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  
  12. 12. How  to  start  parsing  XML?  The  NSXMLParserDelegate  provides  you  methods:  
  13. 13. JSON  
  14. 14. 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.  
  15. 15. 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",
  16. 16. 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  
  17. 17. 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"]]
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×