<?xml version = "1.0" encoding="utf-8"?> <Users> <User id="001"> <name>Ryan</name> <age>24</age> </User> <User id ="002"> <name>Tang</name> <name>xiaohei</name> <age>23</age> </User> </Users>
// // main.m // OC1-XML文件解析 // // Created by qianfeng on 15/6/25. // Copyright (c) 2015年 qianfeng. All rights reserved. // #import <Foundation/Foundation.h> #import "GDataXMLNode.h" //xml 由xml文档头(文档头 <? ?>包含) 文档的主体 //1.添加libxml2.dylib库(系统库), 在bulid Phases中添加二进制库 (在link Binary With libraries 选项) //2.在bulid Setting中 添加头文件路径, search paths 下的 header search paths -->/usr/include/libxml2 //3.添加GDataXMLNode库(第三方库) arc mrc 混合编程 (在bulid Phases下 complie sources GDataXMLNode.m 文件) -fno-objc-arc // int main(int argc, const char * argv[]) { @autoreleasepool { NSData *xmlData =[NSData dataWithContentsOfFile:@"/Users/qianfeng/Desktop/text/text/untitled folder/内存管理/OC1-XML文件解析/en.lproj/users.xml"]; //创建document对象 GDataXMLDocument *document =[[GDataXMLDocument alloc] initWithData:xmlData options:0 error:nil]; //获取文件的根节点 //GDataXMLElement *rootElement =[GDataXMLElement elementWithName:@"Users"];//获取xml的一个节点. GDataXMLElement *rootElement =[document rootElement];//获取XML文件的根节点. NSArray *users =[rootElement elementsForName:@"User"]; //NSLog(@"users=%@",users); //GDataXMLElement *user 节点 形式 存放在users数组中 for (GDataXMLElement *user in users) { NSString *ID =[[user attributeForName:@"id"] stringValue]; NSLog(@"id =%@",ID); // <User id ="002"> // <name>Tang</name> // <name>xiaohei</name> // <age>23</age> // </User> NSString *name =[[[user elementsForName:@"name"] firstObject] stringValue]; NSLog(@"name = %@",name); NSString *age =[[[user elementsForName:@"age"] firstObject] stringValue]; NSLog(@"age = %@",age); } } return 0; }
时间: 2024-10-13 05:17:26