how to Download and save xml in documents and then how to parse xml file from documents?

前端 未结 2 1070
难免孤独
难免孤独 2021-01-01 05:55

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 s

相关标签:
2条回答
  • 2021-01-01 06:14

    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?

    0 讨论(0)
  • 2021-01-01 06:27

    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]
    
    0 讨论(0)
提交回复
热议问题