views:

637

answers:

2

NSXMLParser has three types of init.

-> init

-> initWith Data

-> initWithContents of URL


=> But my xml file is stored at Application's Document directory, so how to parse a file which is stored at "Doc Dir"

Thanks in advance.

+3  A: 

nsdata can be initialize with the contents of a file. Use the initWithContentsOfFile instance method with NSDAta and supply that to nsxmlparser's initWithData method:

NSString * filePath = [[NSBundle mainBundle] pathForResource:@"someFile"
                                                      ofType:@"xml"];
NSData * fileData = [NSData dataWithContentsOfFile:filePath];
NSXMLParser * parser = [NSXMLParser initWithData:fileData];
ennuikiller
Thanks Sir, I completely forgot that. Thanks for helping me.
sugar
You beat me to it, so I added my code to your answer. Please edit as you see fit. :)
e.James
Thanks eJames, I was too lazy to spell it out
ennuikiller
A: 

Well I have an interesting one for you all.

'xmlFileName = [NSString stringWithString:@"broomsticks.xml"];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:xmlFileName]; NSData *xmlFile = [[NSData alloc] initWithContentsOfFile:path]; //NSLog(@"- %@", [[NSString alloc] initWithData:xmlFile encoding:NSUTF8StringEncoding]); xmlParser = [[NSXMLParser alloc] initWithData:xmlFile];'

When I uncomment the NSLog, I get the full xml sheet. But the parser sends everything out as NULL. I think the problem is here as all the other parsing I am doing is pretty much exactly the same, except that all my other parsing is done at download, not from a saved xml file. Any ideas?

Probably you should better ask this as a new question, not post it as an answer... (The "Ask Question" button is in the top right of the page)
sth