DOM解析

#import "secondent.h"

#import "GDataXMLNode.h"

#import "student.h"

@interface secondent ()

@property(nonatomic,strong)NSMutableArray *array;

@end

@implementation secondent

-(NSMutableArray *)array{

//懒加载

if (!_array) {

_array =[NSMutableArray array];

}return _array;

}


- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

}

-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{

NSLog(@"DOM解析");

NSString *filepath =[[NSBundle mainBundle]pathForResource:@"XML_stu" ofType:@"txt"] ;

NSData *data =[NSData dataWithContentsOfFile:filepath];

//3.创建GD xml document 对象  . 此时 xml 文件内的 所有 界点以形式存在 GDxmldocument z中

GDataXMLDocument *xmldocument =[[GDataXMLDocument alloc]initWithData:data options:0 error:nil];

//4.获取到xml文件的根节点  //g根节点里面包含了xml 多有数据

GDataXMLElement *rootelement =xmldocument.rootElement;

//    NSLog(@"%@",rootelement);

//    NSLog(@"%@",rootelement.children);

//5.遍历根节点的所有数据

for (GDataXMLElement  *supelement in rootelement.children) {

//创建学生对象

student *stu =[[student alloc]init];

//遍历子节点,取出子节点中的name gender age hobbyt 然后赋值给stu

for (GDataXMLElement *childelement in supelement.children) {

NSLog(@"节点名%@对应的值%@",childelement.name,childelement.stringValue);

[stu setValue:childelement.stringValue forKey:childelement.name];

}

[self.array addObject:stu];

NSLog(@"%@",self.array);

}

}

你可能感兴趣的:(DOM解析)