Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-24-2008, 05:43 AM   #1 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default Working with XML (?)

Hi,

I am working on a project that parses some xml from a webpage.

Do anyone know of a good tutorial for parsing xml and using its elements in code ?


Thanks
bergetun is offline   Reply With Quote
Old 08-24-2008, 05:53 AM   #2 (permalink)
Registered Member
 
Join Date: May 2008
Posts: 20
Default I found from AppleBlog

iPhone SDK Tutorial: Build a Simple RSS reader for the iPhone | The Apple Blog
odbc2000 is offline   Reply With Quote
Old 08-24-2008, 06:20 AM   #3 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default


Thank you.

I heard some rumors that NSXML wasn't available on the iphone (Only on the simulator )

Is that true ?
bergetun is offline   Reply With Quote
Old 08-24-2008, 07:20 AM   #4 (permalink)
Registered Member
 
Join Date: May 2008
Posts: 20
Default

Quote:
Originally Posted by bergetun View Post
Thank you.

I heard some rumors that NSXML wasn't available on the iphone (Only on the simulator )

Is that true ?
It's not TRUE, NSXML is available both on the simulator and the device.
odbc2000 is offline   Reply With Quote
Old 08-24-2008, 08:03 AM   #5 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

Quote:
Originally Posted by odbc2000 View Post
It's not TRUE, NSXML is available both on the simulator and the device.
Okay. Thanks for the help.
bergetun is offline   Reply With Quote
Old 08-24-2008, 01:37 PM   #6 (permalink)
I live @ iDevKit.com
 
Join Date: Jul 2008
Posts: 142
Default

Here is what is going on with NSXML:
NSXMLDocument has been removed from the SDK because it is processor intensive. NSXMLParser has been left over. If you try using NSXMLDocument on the simulator it will work fine because the simulator uses the standard Cocoa Foundation classes. But as soon as you build for the device NSXMLDocument will not function.

Max
mxweas is offline   Reply With Quote
Old 08-25-2008, 01:49 AM   #7 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

Quote:
Originally Posted by mxweas View Post
Here is what is going on with NSXML:
NSXMLDocument has been removed from the SDK because it is processor intensive. NSXMLParser has been left over. If you try using NSXMLDocument on the simulator it will work fine because the simulator uses the standard Cocoa Foundation classes. But as soon as you build for the device NSXMLDocument will not function.

Max
Thanks for the explanation.
bergetun is offline   Reply With Quote
Old 08-25-2008, 05:49 PM   #8 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

Hi again,

Can anyone please explain a bit more on this tutorial :
Parse XML to custom objects : Atrexis Blog: iPhone - Parse XML to custom objects


What I don't understand how I can cast the XMLToObjectParser (myParser) to a Contact object array.

Lets say I want to loop trough the contacts and print all the lastName information.


Edit: Reading my own post, made me realize that I really have some more reading to do .. (hurry up Amazon)

Last edited by bergetun; 08-25-2008 at 05:52 PM.
bergetun is offline   Reply With Quote
Old 08-25-2008, 06:11 PM   #9 (permalink)
Registered Member
 
Join Date: Aug 2008
Location: London
Posts: 21
Default

You set the delegate. Then listen for three calls:

Code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
You also need an NSMutableArray, let's call it textInProgress.
  • In didStartElement you just clear textInProgress to be an empty NSMutableArray.
  • In didEndElement you can check what the elementName is and act accordingly, using string as the part between the element beginning and end.
  • In foundCharacters just do something like this: [textInProgress appendString:string];
hypercrypt is offline   Reply With Quote
Old 08-26-2008, 02:39 AM   #10 (permalink)
New Member
 
Join Date: Aug 2008
Posts: 31
Default

Quote:
Originally Posted by hypercrypt View Post
You set the delegate. Then listen for three calls:

Code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string;
You also need an NSMutableArray, let's call it textInProgress.
  • In didStartElement you just clear textInProgress to be an empty NSMutableArray.
  • In didEndElement you can check what the elementName is and act accordingly, using string as the part between the element beginning and end.
  • In foundCharacters just do something like this: [textInProgress appendString:string];


But when I browse the myParser with the "object browser" I see that myParser contains a NSCFArray with all my Contacts.

NSURL *url = [NSURL URLWithString: @"http://localhost/contacts.xml"];
XMLToObjectParser *myParser = [[XMLToObjectParser alloc] parseXMLAtURL:url toObject:@"Contact" parseError:nil];

Shoulnd't it be possible to cast this to a "normal" array and then I can work with it
bergetun is offline   Reply With Quote
Old 11-11-2009, 06:36 PM   #11 (permalink)
Registered Member
 
Join Date: Nov 2009
Posts: 2
Default TBXML

Check out TBXML V1.2 for a super-fast, lightweight, easy to use XML parser!
tpbradley is offline   Reply With Quote
Reply

Bookmarks

Tags
tutorial, xml, xml parsing

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 285
21 members and 264 guests
2WeeksToGo, ADY, apatsufas, ckgni, dacapo, Dani77, Fritzer, ghost, HDshot, headkaze, jakerocheleau, joeallenpro, masc2279, mer10, mystic.purple, objch, Rudy, tathaastu, themathminister, timle8n1, Zool
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,878
Threads: 89,224
Posts: 380,732
Top Poster: BrianSlick (7,129)
Welcome to our newest member, olga2000
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:39 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0