iPhone访问本地XML文件 [英] iPhone access local XML file
问题描述
我有一个iPhone应用程序,可以从URL进行XML解析.我的资源目录(在XCode中)有一个我要使用的sample.xml文件.
I have an iPhone app that does XML parsing from a URL. I have a sample.xml file in my Resources directory (in XCode) that I'd like to use.
如何在代码中引用此文件?我尝试使用@"sample.xml"作为URL,但似乎无法找到它.
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.
推荐答案
首先,请小心传递URL作为路径,反之亦然-您可能无法轻松地将两者互换.如果需要,请尝试在文件的路径前加上 file://
.
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://
.
对于查找路径,您可以这样使用NSBundle方法 pathForResource:ofType:
:
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];
这篇关于iPhone访问本地XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!