iPhone access local XML file
I have an iPhone app that does XML parsing from a URL. I have a sample.xml file in my Resources dire开发者_StackOverflow中文版ctory (in XCode) that I'd like to use.
How do I reference this file in code? I've tried @"sample.xml" as the URL and it doesn't appear to be able to find it.
First off, be careful passing a URL as a path and vice-versa - you may not be able to readily interchange the two. If you must, try prefixing the path of the file with file://
.
As for finding the path, you can use the NSBundle method pathForResource:ofType:
as such:
NSString *xmlFilePath = [[NSBundle mainBundle] pathForResource:@"sample"
ofType:@"xml"];
NSString *xmlFileContents = [NSString stringWithContentsOfFile:xmlFilePath];
精彩评论