如何解析iPhone本地存储的xml文件?

发布于 2024-12-10 20:53:43 字数 1382 浏览 8 评论 0原文

我对 iPhone 开发非常陌生。我的资源文件夹中有一个 xml 文件,然后我的客户将每天/每周更新该 xml 文件。我想从他们的 URL(来自客户端)存储新的 xml 文件,并将新的 xml 文件存储在本地,同时我需要删除存储在资源文件夹中的旧文件。我已经遵循了下面的代码,但我现在正在努力检索存储的新 xml 以及如何解析该新 xml 文件。

NSArray *xmlpaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachdirectory = [[NSString alloc] initWithString:[xmlpaths objectAtIndex:0]];

//first save your file from resource directory to app's cache  directory
NSString *path = [[NSBundle mainBundle] pathForResource:@"Directory" ofType:@"xml"];
NSData *filedata = [NSData dataWithContentsOfFile:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *saveName = [cachdirectory stringByAppendingPathComponent:@"NewDirectory.xml"];
[fileManager createFileAtPath:saveName contents:filedata attributes:nil];       

//remove file from resource directory
[fileManager removeItemAtPath:path error:nil];

//new file from internet is avilable then do following:first remove old file from cache
[fileManager removeItemAtPath:cachdirectory error:nil];

//save new file from internet
NSData *newfiledata=[NSData dataWithContentsOfURL:[NSURL URLWithString:UrlString]];
[fileManager createFileAtPath:savename contents:newfiledata attributes:nil];

我已经遵循了这个代码。现在正在努力如何读取/解析这个新的 xml(来自 Url 并保存在文件管理器中)。有人可以帮我吗?感谢您与我一起度过宝贵的时间。请有任何建议或示例代码来解决我的问题吗?我再次感谢您阅读我蹩脚的英语。

Am very new to iPhone development. I have a xml file in Resource folder, then my client will update the xml file in every day/week. I want to store the new xml file from their URL(from client) and store that new xml file in locally at the same time i need to delete the old one which is stored in Resource folder. I have followed below code, but i struggling now to retrieve the stored new xml and how to parse that new xml file.

NSArray *xmlpaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachdirectory = [[NSString alloc] initWithString:[xmlpaths objectAtIndex:0]];

//first save your file from resource directory to app's cache  directory
NSString *path = [[NSBundle mainBundle] pathForResource:@"Directory" ofType:@"xml"];
NSData *filedata = [NSData dataWithContentsOfFile:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *saveName = [cachdirectory stringByAppendingPathComponent:@"NewDirectory.xml"];
[fileManager createFileAtPath:saveName contents:filedata attributes:nil];       

//remove file from resource directory
[fileManager removeItemAtPath:path error:nil];

//new file from internet is avilable then do following:first remove old file from cache
[fileManager removeItemAtPath:cachdirectory error:nil];

//save new file from internet
NSData *newfiledata=[NSData dataWithContentsOfURL:[NSURL URLWithString:UrlString]];
[fileManager createFileAtPath:savename contents:newfiledata attributes:nil];

I have followed this code. Now am struggling how to read/Parse this new xml(From Url and saved in fileManager). Can anyone help me please? Thanks for spending your valuable time with me. Please any suggestion or any sample code to solve my problem? Once again i thank you for read my poor english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一生独一 2024-12-17 20:53:43

我建议最简单的 XML 解析是使用 TouchXML。查看他们的文档

My recommendation for the most simple parsing of XML is to use TouchXML. Check out their docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文