NSXMLDocument讀取遠端XML檔案

tigerhu76發表於2012-05-18

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 {
   //     
}


returnValue輸出的值應為:test


XML檔案格式:

<root>
    <message>test</message>
</root>


相關文章