iPhone access local XML file

馋奶兔 提交于 2019-12-24 00:38:35

问题


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.

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.


回答1:


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];


来源:https://stackoverflow.com/questions/1904527/iphone-access-local-xml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!