如何下载并保存文档中的xml,然后如何从文档中解析xml文件?

发布于 2024-09-08 03:38:44 字数 83 浏览 2 评论 0原文

在我的应用程序中,我必须解析从互联网下载的 xml 文件。如何下载此 xml 并保存在 iPhone 上的文档中?那么我如何开始解析文档中保存的XML?

In my app app i have to parsing a xml file downloaded from internet. How to download this xml and save in documents on iphone ? and then how can i start the parsing of XML saved in documents??

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

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

发布评论

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

评论(2

朮生 2024-09-15 03:38:44

你的意思是这样的

NSString *URLString = @"http://www.example.com/XML/note.xml";
NSURL *URL = [NSURL URLWithString:
              [URLString stringByAddingPercentEscapesUsingEncoding:
               NSASCIIStringEncoding]];


NSData *dataXML = [NSData dataWithContentsOfURL:URL];

NSString *applicationDocumentsDir = 
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"sample.xml"];
[dataXML writeToFile:storePath atomically:TRUE];
    NSData *contenuto = [NSData dataWithContentsOfFile:storePath];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:contenuto]

You mean something like that

NSString *URLString = @"http://www.example.com/XML/note.xml";
NSURL *URL = [NSURL URLWithString:
              [URLString stringByAddingPercentEscapesUsingEncoding:
               NSASCIIStringEncoding]];


NSData *dataXML = [NSData dataWithContentsOfURL:URL];

NSString *applicationDocumentsDir = 
[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *storePath = [applicationDocumentsDir stringByAppendingPathComponent:@"sample.xml"];
[dataXML writeToFile:storePath atomically:TRUE];
    NSData *contenuto = [NSData dataWithContentsOfFile:storePath];
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:contenuto]
爱要勇敢去追 2024-09-15 03:38:44

第一次加载时将内容下载到 NSData 对象中。 其中之一将其保存到磁盘

– writeToFile:atomically:
– writeToFile:options:error:

使用NSData 类参考中描述的 : http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/ NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/writeToFile:atomically

当你想从磁盘加载时,使用[NSData dataWithContentsOfFile:]加载再次将其转换为 NSData 对象。

至于将 NSData 解析为 XML,请查看这个非常非常常见的问题的其他答案,例如 How do我在 Objective-C 中解析包含 XML 的 NSString?

Download the contents to an NSData object when it's loaded for the first time. Save it to disk using either one of

– writeToFile:atomically:
– writeToFile:options:error:

Which are described in the NSData class reference here: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/occ/instm/NSData/writeToFile:atomically:

When you want to load it from disk, use [NSData dataWithContentsOfFile:] to load it into an NSData object again.

As for parsing NSData into XML, look into other answers to this very, very common question, e.g. How do I parse an NSString containing XML in Objective-C?

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