2

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
ferry
  • 69
  • 1
  • 5
  • 11
  • possible duplicate of [NSXMLParser on the iPhone, how do i use it given a xml file (newb here :\)](http://stackoverflow.com/questions/2964503/nsxmlparser-on-the-iphone-how-do-i-use-it-given-a-xml-file-newb-here) – jscs May 18 '11 at 06:38
  • Further details: [Parsing an XML File Stored in the Documents Directory](http://stackoverflow.com/questions/1324404/parsing-an-xml-file-stored-in-the-documents-directory-of-an-iphone-application) [Parsing a Local XML file](http://stackoverflow.com/questions/4918394/parsing-a-local-xml-file-in-ios-sdk) – jscs May 18 '11 at 06:40

3 Answers3

2

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

j_freyre
  • 4,623
  • 2
  • 30
  • 47
1

Instead of the url you can use this

NSURL *baseURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"temp" ofType:@"xml"]];
NSURL *baseURL=[NSURL URLWithString:url];
Tendulkar
  • 5,550
  • 2
  • 27
  • 53
1

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

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
Sarah
  • 1,595
  • 3
  • 25
  • 34