XML parsing on the iPhone

I wanna do some simple XML parsing for the iPhone. Mostly for practice. I think that XML is really easy and i wanna learn how to integrate it with other languages.

i used this link for a tutorial on how do XML parsing, but it was a little bit to advanced for my purpose :/

isn't there a simple way that does not require many lines of code to do XML parsing?

http://webcache.googleusercontent.com/search?q=cache:4S0UStrJP28J:gigaom.com/apple/tutorial-build-a-simple-rss-reader-for-iphone/+xml+parser+iphone&cd=3&hl=en&ct=clnk&client=safari

Best regards!

Kristian

EDIT:

I was able to achieve what i wanted with this:

- (void)startParsing {
    NSData *xmlData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.vegvesen.no/trafikk/xml/savedsearch.xml?id=604"]];
    NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:xmlData] autorelease];
    [parser setDelegate:self];
    [parser parse];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Started %@", elementName);
}

this will just print out the elementName... how should i go on from here to get the value from each element?

Upvotes: 1

Views: 276

Answers (2)

Kasaname
Kasaname

Reputation: 1501

i suppose you must have made a model class like this :

import

CarDetails.h

@interface CarDetails : NSObject
{
    NSString *MapName;
    NSString *MapContentName;
    NSString *MapWidth;
    NSString *MapHeight;
}

@property(retain,nonatomic)NSString *MapName;
@property(retain,nonatomic)NSString *MapContentName;
@property(retain,nonatomic)NSString *MapWidth;
@property(retain,nonatomic)NSString *MapHeight;

CarDetails.m

import "CarDetails.h"

@implementation CarDetails

@synthesize MapName; @synthesize MapContentName; @synthesize MapWidth,MapHeight;

@end

See these are the model objects where u store ur values globally and you can use the values from here

now we use three xml parser delegates

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
   if([elementName isEqualToString:@"car"]){
    carDetails =[[carDetails alloc]init];
}

}


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

if([elementName isEqualToString:@"title"]){
    car.mapName=nodecontent;
}
else if([elementName isEqualToString:@"link"]){
    car.MapContenName=nodecontent;
}

//finally when we reaches the end of tag i am adding data inside the NSMutableArray
if([elementName isEqualToString:@"item"]){

    [rssOutputData addObject:car]; // adding the model class object to the array and   then use this array to retrieve the values
    [car release];
    car = nil;
}
//release the data from mutable string variable
[nodecontent release];

//reallocate the memory to get new content data from file
nodecontent=[[NSMutableString alloc]init];

}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
   currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

1) in didStart you will allocate the model class object only if you find the very first node 2) in foundCharacters you append the data 3) in didEndElement you will assigh the data from foundCharacters to the model object

Upvotes: 0

Kreegr
Kreegr

Reputation: 149

While it's good to know, in practice you might have a much easier time with JSON

Upvotes: 1

Related Questions