doh
doh

Reputation: 387

Reading local XML file makes NSXMLParser return NULL

I have a local xml file which I know contains UTF-8 well-formed xml. But NSXMLParser will not parse correclty.

Path to file:

 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"example.xml"];

Saving the xml from web:

 NSURL *xml = [NSURL URLWithString:@"http://www.example.com/example.xml"];
 NSString *data = [[NSString alloc] initWithContentsOfURL:xml encoding:NSUTF8StringEncoding error:nil];
 [data writeToFile:appFile atomically:YES encoding:NSUTF8StringEncoding error:nil];

Parsing the xml:

 NSURL *xmlFile = [NSURL URLWithString:appFile];
 NSXMLParser *addressParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlFile];

If I try parsing the NSURL "xml" directly it works, but the code above will return NULL and no errors. I found another post here on the forum describing the exact same thing, but I cannot find it again, and the post had no answers.

Hope someone can help! Basically it is just a matter of storing a web xml to file, and then feeding it to NSXMLParser.

Upvotes: 2

Views: 3245

Answers (1)

user467105
user467105

Reputation:

Change this line:

NSURL *xmlFile = [NSURL URLWithString:appFile];

to this:

NSURL *xmlFile = [NSURL fileURLWithPath:appFile];

Because appFile is a local file system path and not an internet URL.

Upvotes: 6

Related Questions