技术探索

NSXMLDocument读取远程XML文件

2013-05-04
1209

不管是在windows、unix或apple环境中进行程序开发,xml文件的存取总是少了的。这里介绍一种在apple环境中使用NSXMLDocument读取远程xml文件的方法。

文件读取代码:

NSError *error;
NSURL *url = [NSURL URLWithString:@"http://www.corp.com/info.xml"];
NSString *returnValue = @"";
@try {
    NSXMLDocument *doc = [[NSXMLDocument alloc] initWithContentsOfURL:url options:0 error:&error];
    NSArray *arrs = [doc nodesForXPath:@"/root/message" error:&error];
    returnValue = [[arrs objectAtIndex:0] stringValue];
}
@catch (NSException *exception) {
    returnValue = exception.reason;
}
@finally {
   //
}



info.xml文件格式:

  
    test  
 



执行以上代码后 returnValue 输出的值应为 test 。