- 获取解析文件路径
NSString * xmlPath = [[NSBundle mainBundle] pathForResource:@"Person" ofType:@"xml"];
2.初始化xml字符串
NSString * xmlStr = [NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:nil];
3.初始化一个GDataXMLDocument对象 , 因为解析时所有的内容都是从该对象中获取,(所以需要将要解析的内容放入该对象)
GDataXMLDocument * document = [[GDataXMLDocument alloc]initWithXMLString:xmlStr options:0 error:nil];
XPath 只要给定相对路径即可比如: //name .只要能到达name,都可以把name获取
获取所有的name节点
NSArray * nameElement = [document nodesForXPath:@"//name" error:nil];
获取所有gender节点
NSArray * genderElement = [document nodesForXPath:@"//gender" error:nil];
通过循环取出每个节点
for (int i = 0; i < 3; i ++) {
GDataXMLElement * nameEle = nameElement[i];
GDataXMLElement * genderEle = genderElement[i];
取节点属性中的字符串
NSString * a = [[nameEle attributeForName:@"name"] stringValue];
取节点字符串
NSString * name = [nameEle stringValue];
NSLog(@"aaa ddddd%@",a);
}