DOM
/**
* GDataXMLNode 是由 Google 提供的开源的机遇DOM工作原理的XML 解析方式, 底层是基于libxml12动态链接库为封装的使用方式, 因此效率高
使用该类的准备工作
1.build Phases -- 添加动态链接库 libxml2.2.dylib
2.build Settings -- header search path -- 添加 /usr/include/libxml2
*/
//Dom解析
- (IBAction)handleRootParser:(UIBarButtonItem *)sender {
[self.dataSource removeAllObjects];
// 1.获取要解析的xml 的路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student.xml" ofType:nil];
// 2.根据文件路径初始化 NSString 对象
NSString *xmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
// 3.将解析内容读到 GDataXMLDocument 中
GDataXMLDocument *document = [[GDataXMLDocument alloc]initWithXMLString:xmlString options:0 error:nil];
// 4.获取根节点 GDataXMLElement 节点对象, GDataXMLNode 属性对象
GDataXMLElement *rootElement = [document rootElement];
// 5.获取根节点下所有的student 节点
NSArray *stuElements = [rootElement elementsForName:@"m:Student"];
// 6.获取student节点下的子节点
for (GDataXMLElement *stuEle in stuElements) {
//获取student 节点下的 name节点
GDataXMLElement *nameElement = [[stuEle elementsForName:@"m:name"] firstObject];
GDataXMLElement *genderElement = [[stuEle elementsForName:@"m:gender"] firstObject];
GDataXMLElement *ageElement = [[stuEle elementsForName:@"m:age"] firstObject];
GDataXMLElement *phoneElement = [[stuEle elementsForName:@"m:phone"] firstObject];
GDataXMLElement *mottoElement = [[stuEle elementsForName:@"m:motto"] firstObject];
Student *stu = [[Student alloc]init];
stu.name = [nameElement stringValue];
stu.gender = [genderElement stringValue];
stu.age = [ageElement stringValue];
stu.phone = [phoneElement stringValue];
stu.motto = [mottoElement stringValue];
//获取属性将节点
GDataXMLNode *positionNode = [stuEle attributeForName:@"position"];
stu.position = [positionNode stringValue];
[self.dataSource addObject:stu];
[stu release];
}
[self.tableView reloadData];
[document release];
}
- (IBAction)handleXPath:(UIBarButtonItem *)sender {
[self.dataSource removeAllObjects];
// 1.获取要解析的xml 的路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student.xml" ofType:nil];
// 2.根据文件路径初始化 NSString 对象
NSString *xmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
// 3.将解析内容读到 GDataXMLDocument 中
GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil];
//4.获取所有的叶子节点
// 4.1获取所有的name节点
NSArray *nameElement = [document nodesForXPath:@"//m:name" error:nil];
// 4.2获取所有的gender节点
NSArray *genderElement = [document nodesForXPath:@"//m:gender" error:nil];
// 4.3获取所有的age节点
NSArray *ageElement = [document nodesForXPath:@"//m:age" error:nil];
// 4.4获取所有的phone节点
NSArray *phoneElement = [document nodesForXPath:@"//m:phone" error:nil];
// 4.5获取所有的motto节点
NSArray *mottoElement = [document nodesForXPath:@"//m:motto" error:nil];
// 4.6获取student 节点
NSArray *studentElement = [document nodesForXPath:@"//m:Student" error:nil];
//循环次数
int count = (int)nameElement.count;
for (int i = 0; i < count; i++) {
GDataXMLElement *nameEle = nameElement[i];
GDataXMLElement *genderEle = genderElement[i];
GDataXMLElement *ageEle = ageElement[i];
GDataXMLElement *phoneEle = phoneElement[i];
GDataXMLElement *mottoEle = mottoElement[i];
GDataXMLNode *positionEle = [studentElement[i] attributeForName:@"position"];
Student *stu = [[[Student alloc]init] autorelease];
stu.name = [nameEle stringValue];
stu.gender = [genderEle stringValue];
stu.age = [ageEle stringValue];
stu.phone = [phoneEle stringValue];
stu.motto = [mottoEle stringValue];
stu.position = [positionEle stringValue];
[self.dataSource addObject:stu];
[stu release];
}
//刷新界面
[self.tableView reloadData];
[document release];
}