开发者

How to parse an XML file with NSXMLParser

I have a file called sample.xml and I need to retrieve the contents in that file, using NSXMLParser. I tried parsing the XML file by way of a URL earlier,开发者_运维技巧 but now I have an XML file. Can somebody tell me how to parse when I have a file? I am Using Xcode4 and iOS SDK 4.3 Thanks in advance.


When you parsed your data with an URL you should have done something like that:

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfURL:<#(NSURL *)#>]];

// Tell NSXMLParser that this class is its delegate
[parser setDelegate:self];


[parser setShouldProcessNamespaces:NO]; 
[parser setShouldReportNamespacePrefixes:NO]; 
[parser setShouldResolveExternalEntities:NO];   

// Kick off file parsing
[parser parse];

//[parser setDelegate:nil];
[parser release];

To parse a file, you simply have to transform the first line and replace [NSData dataWithContentsOfURL:<#(NSURL *)#>] with [NSData dataWithContentsOfFile:<#(NSString *)#>] where the NSString is the path to your file.

PS: Increase your acceptance rate


Instead of the url you can use this

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"temp" ofType:@"xml"]];
NSURL *baseURL=[NSURL URLWithString:url];


You can check the following tutorials for reading and parsing XML files in Objective-C.

Reading from a XML file in iOS.

Objective C: Parsing an XML file

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜