#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);
}
}