开发者

Can I parse a XML file that is on a web server without downloading the file?

开发者 https://www.devze.com 2023-02-16 23:54 出处:网络
I need to parse a XML file which is located on my server and return the node values without ever downloading the file to the device. Right now, the file is downloading to the device for testing purpos

I need to parse a XML file which is located on my server and return the node values without ever downloading the file to the device. Right now, the file is downloading to the device for testing purposes. Here is my current code:

+ (NSString *)dataFilePath:(BOOL)forSave {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentsPath = [documentsDirectory stringByAppendingPathComponent:@"file.xml"];
    return documentsPath;    
}
+ (NSString *)parse:(NSString *)nodesForPath:(NSString *)elementsForName {

NSString *filePath = [self dataFilePath:FALSE];
NSData *xmlData = [[NSMutableData alloc] initWithContentsOfFile:filePath];
NSError *error;
GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:xmlData options:0 error:&error];
if (doc == nil) { return nil; }
ViewController *view = [[[ViewController alloc] init] autorelease];
NSArray *getVersionInfo = [doc nodesForXPath:nodesForPath error:nil];
for (GDataXMLElement *versionInfo in getVersionInfo) {
    NSArray *elm1 = [versionInfo elementsForName:elementsForName];开发者_C百科
    GDataXMLElement *elm2 = (GDataXMLElement *) [elm1 objectAtIndex:0];
        return elm2.stringValue;
}

[doc release];
[xmlData release];
}

It works fine but it parses the file in the document's directory. How would I set it up to parse directly from the web server? Thanks


The only way I can see to do this would be to run a program on the server to parse the file and return the result to the phone.

For the phone to run the code that parses the file it has to read the file which, by definition, will download it.


You could try using a pull parser for the XML and just work with the data received from the network as a stream, discarding unneeded contents.

You read a few bytes from the response, parse them, process, discard, read some more. You might have to make your own implementation of a pull parser because your dealing with incomplete, hence invalid, XML or to look up a parser that's tolerant to data ending (definitely not a validating one).

It might be worth-while to make your own implementation for the parser if you are dealing with really huge XML files. This way you can only store pieces of the XML that you need (you might need part of the XML 'tree' or none at all or you might need to retain other nodes referenced by another node in your XML).

I'm sorry for the generic answer, but I haven't used XML parsers on iOS yet and I'm not anywhere near a Mac atm to be able to test if NSXMLParser works for you.

Edit: Related post here.


Even if you don't want to save the file to your hard disk, you must have the contents of that file loaded on your RAM.

Convert NSData* to NSString, and then NSString to char* and give that to RapidXML.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号