[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);

}

运行结果:

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念