如何在 Xcode 中解析本地 xml 文件而不是 Web xml? [英] How can i parse local xml file instead of Web xml in Xcode?

查看:64
本文介绍了如何在 Xcode 中解析本地 xml 文件而不是 Web xml?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很容易使用 NSXML Parser 从 Web URL 解析 XML 文件,但我发现解析本地 XML 文件有点困难.?

Hii it's easy for me to parse a XML file from a web URL using NSXML Parser but i found little bit difficulty in parsing the local XML file.?

考虑一下,我的 parser.m 有一个

consider this, my parser.m has a method for

-(void)parseRssFeed:(NSString *)url withDelegate:(id)aDelegate {
[self setDelegate:aDelegate];

responseData = [[NSMutableData data] retain];
NSURL *baseURL = [[NSURL URLWithString:url] retain];


NSURLRequest *request = [NSURLRequest requestWithURL:baseURL];

[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];}

我使用这种方法在我的一个 viewcontroller.m 中调用 RSS 提要

and i used this method for calling a RSS Feed in one of my viewcontroller.m as

- (void)loadData {
if (items == nil) {
    [activityIndicator startAnimating];

    Parser *rssParser = [[Parser alloc] init];
    [rssParser parseRssFeed:@"http://--some RssFeed url---" withDelegate:self];

    [rssParser release];

} else {
    [self.tableView reloadData];
}
}

现在我必须加载我的本地 XML 文件,而不是某些 Web Rss Feed Url.请帮助我进行此编码,我必须更改并为我的本地 XML 文件包含 NSBundlePath 资源.

and now instead of the some web Rss Feed Url i have to load my local XML file. Please help me in this coding where i have to change and include NSBundlePath Resource for my local XML FIle.

提前致谢!!!

推荐答案

对于本地文件,您不需要 NSURLConnection 来获取数据.下面将把一个名为local.xml"的本地 XML 文件放入一个 NSData 对象中:

For a local file you don't need the NSURLConnection to get the data. The following will get a local XML file named "local.xml" into an NSData object:

NSString *path = [[NSBundle mainBundle] pathForResource:@"local" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:path];

此时,您应该能够调用与远程 xml 文件中的数据相同的解析代码.

At this point you should be able to call the same parsing code you use once you have the data from the remote xml file.

这篇关于如何在 Xcode 中解析本地 xml 文件而不是 Web xml?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆