[cocoa]_[初级]_[解析XML文件获取数据]

前端之家收集整理的这篇文章主要介绍了[cocoa]_[初级]_[解析XML文件获取数据]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

场景:获取配置文件中某些数据用于程序某个功能的应用。

+(void) OpenXmlFile
{
    NSString *optionPath =@"/Users/mac/work/pratise/12.xml";
    NSURL *furl = [NSURL fileURLWithPath:optionPath];
    NSXMLDocument * xmlDoc = [[NSXMLDocument alloc]initWithContentsOfURL:furl
            options:(NSXMLNodePreserveWhitespace|NSXMLNodePreserveCDATA)
                                                    error:nil];
    [xmlDoc autorelease];
    NSXMLElement *root = [xmlDoc rootElement];
    NSXMLElement *family =(NSXMLElement*)[root childAtIndex:0];
    NSLog(@"%@",family.stringValue);
    
    NSXMLElement *friend =(NSXMLElement*)[root childAtIndex:1];
    NSLog(@"%@",friend.stringValue);
    
    NSXMLElement *classmate =(NSXMLElement*)[root childAtIndex:2];
    NSLog(@"%@",classmate.stringValue);
    
    NSXMLElement *worker =(NSXMLElement*)[root childAtIndex:3];
    NSLog(@"%@",worker.stringValue);
    
    NSXMLElement *fellowVillager =(NSXMLElement*)[root childAtIndex:4];
    NSLog(@"%@",fellowVillager.stringValue);
    
    NSXMLElement *stranger =(NSXMLElement*)[root childAtIndex:5];
    NSLog(@"%@",stranger.stringValue);

}

运行结果:

原文链接:https://www.f2er.com/xml/296229.html

猜你在找的XML相关文章