NSXML Parser Delegate Mathods

1 ) How to get Local File path ?
Ans : NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@”myFile.xml”];

2 ) How to Initiate Parser ?
Ans :
– (void)parserDidStartDocument:(NSXMLParser *)parser{
NSLog(@”found file and started parsing”);
}

– (void)parseXMLFileAtURL:(NSString *)URL //URL is the file path (i.e. /Applications/MyExample.app/MyFile.xml)
{
//you must then convert the path to a proper NSURL or it won’t work
NSURL *xmlURL = [NSURL fileURLWithPath:URL];

// here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
// this may be necessary only for the toolchain
NSXMLParser *parser = [[ NSClassFromString(@”NSXMLParser”) alloc] initWithContentsOfURL:xmlURL];

// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
[parser setDelegate:self];

// Depending on the XML document you’re parsing, you may want to enable these features of NSXMLParser.
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];

[parser parse];

[parser release];
}

3 ) Three Delegates Method to parse xml file ..

– (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
NSLog(@”found this element: %@”, elementName);
}

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

– (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
NSLog(@”Found character : %@” , string);
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s