xml解析 -- GData
XML解析有两种情况,第一种是不带名称空间的XML,第二种是带名称空间的XML。 解析XML使用的是谷歌提供的第三方库:GData。在GData中使用最多的就是XPath语法解析,XPath语法解析针对两种格式的XML有不同的解析方式。
XPath返回的结果是一个数组。
不带名称空间的XML: xml.txt <root> <books> <book id="1" language="ch" num="2"> <name>甄嬛传</name> <auther> <name>流潋紫</name> </auther> <price>80.00</price> <summary>一部宫廷情感大戏,口碑极佳,被称为宫廷剧的终结篇,是一部具有里程碑意义的经典之作。在日本,韩国,美国等相继播出。</summary> </book> <book id="2" language="ch"> <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>
NSString *xmlPath=[[NSBundle mainBundle] pathForResource:@"xml" ofType:@"txt"]; NSString *xmlStr=[NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:nil]; //使用谷歌提供的第三方库GData解析XML //文档类 GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil]; //取根节点 GDataXMLElement *rootElement=doc.rootElement; //拿到books节点 GDataXMLElement *booksElement=(GDataXMLElement *)[rootElement childAtIndex:0]; //遍历books节点的子节点 for (GDataXMLElement *book in [booksElement children]) { GDataXMLElement *nameElement=[[book elementsForName:@"name"] lastObject]; NSLog(@"%@-%@",nameElement.name,nameElement.stringValue); GDataXMLElement* languageElement=(GDataXMLElement* )[book attributeForName:@"language"]; //xmlString 打印出原型 language="ch" NSLog(@"%@",languageElement.XMLString); //stringValue 打印出元素的指 ch NSLog(@"%@",languageElement.stringValue); } 使用XPath语法解析:(常用) / 绝对路径 | 多路径 // 相对路径
//name 找出所有名为 name 的节点 //books/book[1]/name 找出第一本书的书名 //books/book[last()]/name 找到最后一本书名 //books/book[@num]/name 找到book中必须包含num的属性 //books/book[@id=3]/name 找到book中属性为id,它的值为3 解析代码:
NSString *xmlPath=[[NSBundle mainBundle] pathForResource:@"xml" ofType:@"txt"]; NSString *xmlStr=[NSString stringWithContentsOfFile:xmlPath encoding:NSUTF8StringEncoding error:nil]; //使用谷歌提供的第三方库GData解析XML //文档类 GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithXMLString:xmlStr options:0 error:nil]; /* XPath 语法: / 绝对路径 | 多路径 // 相对路径 /********/ //name 找出所有名为 name 的节点 //books/book[1]/name 找出第一本书的书名 //books/book[last()]/name 找到最后一本书名 //books/book[@num]/name 找到book中必须包含num的属性 //books/book[@id=3]/name 找到book中属性为id,它的值为3 /********/ NSArray *ary=[doc nodesForXPath:@"root/books/book/name | root/books/book/price" error:nil]; NSArray *ary1=[doc nodesForXPath:@"//name" error:nil];//找出所有名为 name 的节点 NSArray *ary2=[doc nodesForXPath:@"//books/book[1]/name" error:nil];//混合使用 NSArray *ary3=[doc nodesForXPath:@"//books/book[last()]/name" error:nil];//混合使用 NSArray *ary4=[doc nodesForXPath:@"//books/book[@num]/name" error:nil];//混合使用 NSArray *ary5=[doc nodesForXPath:@"//books/book[@id=3]/name" error:nil];//混合使用 for (GDataXMLElement *elm in ary5) { NSLog(@"%@",elm.XMLString); } 带有名称空间的XML解析: 首先正对XML来说 什么是名称空间?
名称空间,也就是命名空间 关键字:xmlns -》xml namespace 名称空间必须是一个地址url(网址 例如:http://www.baidu.com)
这样一来,元素应该这样书写: <"http://www.baidu.com":name>XXX</name>
但是这样就很麻烦,所有我们将名称空间取别名 xmlns表示要声明一个名称空间 <root xmlns:book="http://www.baidu.com"> 那么这样一来,元素就可以这样写 <book:name>XXX</name> 缺省名称空间,只要没显示声明属于哪个名称空间的节点,那么它就属于缺省名称空间 使用xmlns来声明一个缺省名称空间 <root xmlns="http://www.sina.cn"> 那么元素为:<name>XXXX</name> 注意一点:缺省名称空间必须写上xmlns:
NSString *str1=[[NSBundle mainBundle] pathForResource:@"xmlns2" ofType:@"txt"]; NSString *dataXml=[NSString stringWithContentsOfFile:str1 encoding:NSUTF8StringEncoding error:nil]; GDataXMLDocument *docXmlns=[[GDataXMLDocument alloc] initWithXMLString:dataXml options:0 error:nil]; /* <root xmlns:book="http://www.baidu.com" xmlns="http://www.sina.com.cn"> <books> <book id="1" language="ch"> <book:name>甄嬛传</book:name> */ //缺省名称空间必须全部写上 xmlns: GDataXMLElement *bookElem=[[docXmlns nodesForXPath:@"<span style="color:#ff0000;">/xmlns:root/xmlns:books/xmlns:book[@id=3]/book:name</span>" namespaces:@{<span style="color:#ff0000;">@"xmlns":@"http://www.sina.com.cn",@"book":@"http://www.baidu.com"</span>} error:nil] firstObject]; NSLog(@"%@",bookElem.stringValue); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |