1、xml文件(不带命名空间的xml)
<?xml version="1.0" encoding="utf-8" ?>
<root>
<books>
<book id="1" language="ch" code="111">
<name>甄嬛传</name>
<auther>
<name>流潋紫</name>
</auther>
<price>80.00</price>
<summary>一部宫廷情感大戏,口碑极佳,被称为宫廷剧的终结篇,是一部具有里程碑意义的经典之作。在日本,韩国,美国等相继播出。</summary>
</book>
<book id="2" language="ch" isbn="">
<name>呐喊</name>
<auther>
<name>鲁迅</name>
</auther>
<price>12.00</price>
<summary>揭示了中国的社会面貌,控诉了封建制度的罪恶,喊出了“五四”时期革命者的心声。它反映了“五四”彻底不妥协地反封建主义的革命精神,适应了中国革命从旧民主主义向新民主主义转变的需要,在中国现代文化史和文学史上占有重要地位!</summary>
</book>
<book id="3" language="en">
<name>哈利波特</name>
<auther>
<name>JK罗琳</name>
</auther>
<price>365.00</price>
<summary>主人公哈利·波特在霍格沃茨魔法学校六年的学习生活和冒险故事。</summary>
</book>
</books>
</root>
普通解析方法
//path 文件束
NSString *path = [[NSBundlemainBundle]pathForResource:@"xml"ofType:@"txt"];
NSString *xmlStr = [NSStringstringWithContentsOfFile:pathencoding:NSUTF8StringEncodingerror:nil];
//xml文档类
GDataXMLDocument *doc = [[GDataXMLDocumentalloc]initWithXMLString:xmlStroptions:0error:nil];
//根节点 root
GDataXMLElement *roorEle = [docrootElement];
//books 节点 通过索引拿到子节点
GDataXMLElement *booksEle = (GDataXMLElement*)[roorElechildAtIndex:0];
//book 节点 拿到所有子节点
NSArray *bookArray = [booksElechildren];
for (GDataXMLElement *bookElein bookArray) {
//name 节点 通过节点的名称拿到子节点
GDataXMLElement *nameELe = [[bookEleelementsForName:@"name"]lastObject];
//原型
NSLog(@"%@",nameELe.XMLString);
//打印出节点的名称和节点的值
NSLog(@"%@:%@",nameELe.name,nameELe.stringValue);
//auther节点
GDataXMLElement *authorEle = [[bookEleelementsForName:@"auther"]objectAtIndex:0];
NSLog(@"%@:%@",authorEle.name,authorEle.stringValue);
GDataXMLElement *priceEle = [bookEleelementsForName:@"price"][0];
NSLog(@"%@:%@",priceEle.name,priceEle.stringValue);
GDataXMLElement *summaryEle = [bookEleelementsForName:@"summary"][0];
NSLog(@"%@:%@",summaryEle.name,summaryEle.stringValue);
//取出book节点的属性
GDataXMLElement *idAtt = (GDataXMLElement*)[bookEleattributeForName:@"id"];
NSLog(@"%@",idAtt.XMLString);
NSLog(@"%@:%@",idAtt.name,idAtt.stringValue);
//GDataXMLElement 可以表示节点和属性
//表示节点的时候,name代表节点的名称,stringValue代表节点的值
//表示属性的时候,name代表属性的名称,stringValue代表属性的值
}
Xpath解析方法
//path 文件束
NSString *path = [[NSBundlemainBundle] pathForResource:@"xml"ofType:@"txt"];
NSString *xmlStr = [NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];
//xml文档类
GDataXMLDocument *doc = [[GDataXMLDocumentalloc] initWithXMLString:xmlStroptions:0error:nil];
NSArray *array = [docnodesForXPath:@"/root/books/book[@code | @isbn]/name"error:nil];
for(GDataXMLElement *elein array){
NSLog(@"%@",ele.stringValue);
}
/*
XPath 语法
/root/books/book/name | /root/books/book/auther/name 多路径 绝对路径
//name 相对路径
/root/books/book[1]/name 拿到第一个book节点
/root/books/book[last()]/name 拿到最后一个book节点
/root/books/book[position()<3]/name 前两个节点
/root/books/book[@code]/name 包含属性为code的节点
/root/books/book[@id=1]/name 找到id为1的book节点
/root/books/book[@code | @isbn]/name 包含属性code或者isbn的book节点
*/
2、xml文件(带命名空间的xml)
在根元素中如果有上面这行代码的话,就表示这个xml文件是一个带命名空间的xml
xmlns:book=“http://www.baidu.com” 表示book元素的命名空间是 “http://www.baidu.com”
xmlns=“http://www.sina.com.cn” 表示这是个缺省的命名空间,是“http://www/sina.com.cn”,缺省表示如果xml中的 其他元素没有设置命名空间的话,默认就是这个
//path 文件束
NSString *path = [[NSBundlemainBundle] pathForResource:@"xmlns2"ofType:@"txt"];
NSString *xmlStr = [NSStringstringWithContentsOfFile:path encoding:NSUTF8StringEncodingerror:nil];
//xml文档类
GDataXMLDocument *doc = [[GDataXMLDocumentalloc] initWithXMLString:xmlStroptions:0error:nil];
// /xmlns:root/xmlns:books/xmlns:book/book:name
NSDictionary *ns =@{@"book":@"http://www.baidu.com",
@"xmlns":@"http://www.sina.com.cn"};
NSArray *array = [docnodesForXPath:@" /xmlns:root/xmlns:books/xmlns:book/book:name"namespaces:nserror:nil];
for(GDataXMLElement *elein array){
NSLog(@"%@",ele.stringValue);
}
3、自己创建xml文件(带命名空间的xml)
GDataXMLElement *nameEle = [GDataXMLElementelementWithName:@"name"stringValue:@"甄嬛传"];
GDataXMLElement *priceEle = [GDataXMLElementelementWithName:@"price"stringValue:@"80.00"];
GDataXMLElement *bookEle = [GDataXMLElementelementWithName:@"book"];
[bookEleaddChild:nameEle];
[bookEleaddChild:priceEle];
[bookEle addAttribute:[GDataXMLElementattributeWithName:@"id"stringValue:@"1"]];
//root节点
GDataXMLElement *rootEle = [GDataXMLElementelementWithName:@"root"];
[rootEleaddChild:bookEle];
//添加名称空间
[rootEle addNamespace:[GDataXMLElementnamespaceWithName:@""stringValue:@"http://www.baidu.com"]];
GDataXMLDocument *doc = [[GDataXMLDocumentalloc]initWithRootElement:rootEle];
//设定xml编码
[doc setCharacterEncoding:@"utf-8"];
//doc.XMLData就是我们创建好的xml文档
NSLog(@"%@",[[NSStringalloc]initWithData:[docXMLData]encoding:NSUTF8StringEncoding]);