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 01-15-2010, 10:16 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 5
Default Parsing XML without attribute and display it in UITableView

Hello all, I'm newbie in iPhone Application Programming. I can't get value from XML file and display it in UITableView. I need to get the name value of animal. How is the simple way to parse XML without attribute? I've been read NSXMLParser Documentation but the data is not displayed in my UITableView. Here is my XML file :

Code:
<?xml version="1.0" encoding="UTF-8"?>
<animals>
  <animal>
    <id>1</id>
    <name>Elephant</name>
  </animal>
  <animal>
    <id>2</id>
    <name>Tiger</name>
  </animal>
  <animal>
    <id>3</id>
    <name>Bird</name>
  </animal>
</animals>
I follow some tutorial about Parsing XML with attribute, but in my XML file, I don't have attribute. And this is my code to parse XML:

Code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName 
     attributes:(NSDictionary *)attributeDict {

     if([elementName isEqualToString:@"animals"]) {
        appDelegate.animals = [[NSMutableArray alloc] init];
     }

     else if([elementName isEqualToString:@"animal"]) {  
     }

     else if([elementName isEqualToString:@"id"]){
        aAnimal = [[Animal alloc] init];
        aAnimal.animalID = elementName;
     } 
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
     if(!currentElementValue) 
        currentElementValue = [[NSMutableString alloc] initWithString:string];
     else
        [currentElementValue appendString:string]; 
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName 
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

     if([elementName isEqualToString:@"animals"])
        return;
     else if([elementName isEqualToString:@"animal"]) {
     }
     else if([elementName isEqualToString:@"id"]) {
        [appDelegate.animals addObject:aAnimal];      
        [aAnimal release];
        aAnimal = nil;
     }
     else 
        [aAnimal setValue:currentElementValue forKey:elementName];

     [currentElementValue release];
     currentElementValue = nil;
}
funk_ynot_24 is offline   Reply With Quote
Old 01-15-2010, 11:55 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 41
Posts: 1,184
Default

Wow... that sounds like the hard way to do it, unless for some reason you actually need to parse an XML file (like, say, your sample XML file is not your real data, but just a test file you're using for now to get things working?).

Take a look at this tutorial that uses a plist for a data source. Disclaimer: I haven't been through this tutorial myself, nor have I ever populated a UITableView from a plist. I use Core Data with a SQLite database in my own stuff.

UITableView - Drill down table view tutorial - iPhone SDK Articles
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Old 01-16-2010, 02:13 AM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 5
Default

Thanks for reply dljeffery, I follow this tutorial for the first time

Parsing XML Files - iPhone SDK Articles

But that tutorial have attribute on XML file. I try the same case but my XML file don't have attribute. I need to display the name of animal in my UITableView.
funk_ynot_24 is offline   Reply With Quote
Old 01-16-2010, 12:40 PM   #4 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Jan 2010
Location: Issaquah, WA
Age: 41
Posts: 1,184
Default

Well, that article looks like it's probably a good one... but really it's about learning how to parse XML files, whereas it sounds to me like your goal is not to learn to parse XML files, it's just to learn how to get your data into a table view. If that is true, then it would make sense to go with a plist or SQLite data source rather than XML.

The XML parsing just adds unnecessary complexity... it would be kind of like using a CSV file for your data source when you're only ever going to have the one, static CSV file. You could do it if you wanted to, but why?
__________________
Recall It! Tag your notes. Tag your photos. Tag your thoughts. Tag your life.

Recall It! for iPad

http://www.dljeffery.com
dljeffery is offline   Reply With Quote
Reply

Bookmarks

Tags
nsxmlparser, uitableview, xml

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: 243
12 members and 231 guests
ADY, CKAmike, Dani77, Duncan C, HemiMG, nick.keroulis, Promo Dispenser, Punkjumper, Rudy, sacha1996, sneaky, spiderguy84
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

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