//(动态库环境的配置) //1、获取GDataXMLNode.h/m文件,将GDataXMLNode.h/m文件添加到工程中 //2、向工程中增加“libxml2.2dylib”库(最新版) //(下边三步重要) //3、在工程的“Build Settings”页中找到“Header Search Path”项,添加/usr/include/libxml2"到路径中 //4.搜索Other Linker Flags,添加-lxml2(这一步最好加上,否则会出问题) //5.在build phases的GDataXMLNode.m文件给成飞arc环境 //6、添加“GDataXMLNode.h”文件到头文件中,如工程能编译通过,则说明GDataXMLNode添加成功 @interface ViewController () @property (nonatomic, strong) NSMutableArray * classAry; @property (nonatomic, strong) NSMutableArray * stuAry; @property (nonatomic, strong) NSMutableString * tempStr; @property (nonatomic, strong) JRClass * jrClass; @property (nonatomic, strong) JRStudent * jrStu; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } //懒加载 - (NSMutableArray *)classAry { if (!_classAry) { _classAry = [NSMutableArray array]; } return _classAry; } //练习 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSURL * url = [[NSBundle mainBundle] URLForResource:@"laoshi.xml" withExtension:nil]; NSData * data = [NSData dataWithContentsOfURL:url]; GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; GDataXMLElement * root = document.rootElement; NSArray * array = root.children; // 一个e代表一个班级 for(GDataXMLElement * e in array) { GDataXMLNode * node = [e attributeForName:@"name"]; //班级名 self.jrClass = [[JRClass alloc] init]; self.jrClass.className = node.stringValue; //班级成员 self.stuAry = [NSMutableArray array]; self.jrClass.members = self.stuAry; NSArray * array2 = [e elementsForName:@"member"]; for(GDataXMLElement * e2 in array2) { JRStudent * stu = [[JRStudent alloc] init]; [self.stuAry addObject:stu]; GDataXMLNode * node = [e2 attributeForName:@"name"]; stu.stuName = node.stringValue; GDataXMLNode * node2 = [e2 attributeForName:@"age"]; stu.stuAge = [node2.stringValue integerValue]; stu.stuType = e2.stringValue; } //添加班级数组 [self.classAry addObject:self.jrClass]; } NSLog(@"%@", self.classAry); } //进阶用法获,取节点内部的属性 - (void)test4 { //获取xml文件,创建data NSURL *url = [[NSBundle mainBundle]URLForResource:@"xml.xml" withExtension:nil]; NSData * data = [NSData dataWithContentsOfURL:url]; //创建GDataXMLDocument,传递data(返回的文件就包含了所以的xml的内容) GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; //获取GDataXMLDocument文件对象的第一层子节点(根节点) GDataXMLElement * root = document.rootElement; //获取根节点内部的元素 NSArray * array = [root elementsForName:@"part1"]; //获取根节点内部的元素的name和内容 (e的内容 {type:1 name:book xml:"<book>语文</book>"} ) for(GDataXMLElement * e in array) { GDataXMLNode * attr = [e attributeForName:@"name"]; NSLog(@"%@", attr.stringValue); } } //进阶用法(xml有多层) - (void)test3 { //获取xml文件,创建data NSURL *url = [[NSBundle mainBundle]URLForResource:@"xml.xml" withExtension:nil]; NSData * data = [NSData dataWithContentsOfURL:url]; //创建GDataXMLDocument,传递data(返回的文件就包含了所以的xml的内容) GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; //获取GDataXMLDocument文件对象的第一层子节点(根节点) GDataXMLElement * root = document.rootElement; //获取根节点内部的元素 NSArray * array = [root elementsForName:@"part1"]; //获取根节点内部的元素的name和内容 (e的内容 {type:1 name:book xml:"<book>语文</book>"} ) for(GDataXMLElement * e in array) { NSArray * array2 = [e elementsForName:@"book"]; for(GDataXMLElement * e2 in array2) { NSLog(@"%@====%@", e2.stringValue, e2.name); } } } //进阶用法(xml只有一层) - (void)test2 { //获取xml文件,创建data NSURL *url = [[NSBundle mainBundle]URLForResource:@"xml.xml" withExtension:nil]; NSData * data = [NSData dataWithContentsOfURL:url]; //创建GDataXMLDocument,传递data(返回的文件就包含了所以的xml的内容) GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; //获取GDataXMLDocument文件对象的第一层子节点(根节点) GDataXMLElement * root = document.rootElement; //获取根节点内部的元素 NSArray * array = [root elementsForName:@"book"]; //获取根节点内部的元素的name和内容 (e的内容 {type:1 name:book xml:"<book>语文</book>"} ) for(GDataXMLElement * e in array) { NSLog(@"%@====%@", e.stringValue, e.name); } } //基本用法 - (void)test { //获取xml文件,创建data NSURL *url = [[NSBundle mainBundle]URLForResource:@"xml.xml" withExtension:nil]; NSData * data = [NSData dataWithContentsOfURL:url]; //创建GDataXMLDocument,传递data(返回的文件就包含了所以的xml的内容) GDataXMLDocument * document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; //获取GDataXMLDocument文件对象的第一层子节点(根节点) GDataXMLElement * root = document.rootElement; //获取根节点内部的元素 NSArray * array = root.children; //获取根节点内部的元素的name和内容 (e的内容 {type:1 name:book xml:"<book>语文</book>"} ) for(GDataXMLElement * e in array) { NSLog(@"%@", e); NSLog(@"%@====%@", e.stringValue, e.name); } }