【玩转cocos2d-x之三十二】xml的解析

来源:互联网 发布:行助手网络检测未通过 编辑:程序博客网 时间:2024/04/30 15:25

原创作品,转载请标明http://blog.csdn.net/jackystudio/article/details/17409381


cocos2d-x中对xml的解析是采用的TinyXML库,而对plist的解析同时结合了CCDictionary来处理,这里简单介绍下cocos2d-x中解析xml的两种方式,也是常用的xml两个C++解析库:TinyXML和RapidXML。xml被设计用于数据存储和传输,重点是数据内容本身,而不像html,用于表现数据。


1.TinyXML


1.1.概况

TinyXML的主页是http://www.grinninglizard.com/tinyxml/,本来不打算介绍这个库的,因为它的解析效率并不高,但是鉴于cocos2d-x采用的就是它,所以也稍微写一下它的用法,cocos2d-x使用的是TinyXML2,Github地址在https://github.com/leethomason/tinyxml2。就是对TinyXML重新进行了封装,具体的区别在其github上也写的很清楚了。


1.2.示例

这里不介绍API,因为太多了,在线手册也都有。首先下载TinyXML库,将4个cpp文件和2个h文件加入cocos2d-x中。添加头文件时只需添加"tinyxml.h"即可。


1.2.1.xml的创建

[cpp] view plaincopy
  1. void TestLayer::writeTinyXML()  
  2. {  
  3.     TiXmlDocument* myTinyXMLDoc=new TiXmlDocument();//创建文档对象  
  4.   
  5.     TiXmlElement* rootElement=new TiXmlElement("Jacky");//创建根节点并连接到文档  
  6.     myTinyXMLDoc->LinkEndChild(rootElement);  
  7.   
  8.     TiXmlElement* infoElement=new TiXmlElement("Info");//创建info节点并连接到根节点  
  9.     rootElement->LinkEndChild(infoElement);  
  10.   
  11.     infoElement->SetAttribute("Name","Jacky");//为info节点设置属性  
  12.     infoElement->SetAttribute("QQ","754505629");  
  13.     infoElement->SetAttribute("E-mail","Geek.Jacky@Gmail.com");  
  14.   
  15.     TiXmlElement* blogElement=new TiXmlElement("blog");  
  16.     infoElement->LinkEndChild(blogElement);//创建blog节点并连接到info节点  
  17.     TiXmlText* blogtext=new TiXmlText("http://blog.csdn.net/jackystudio");  
  18.     blogElement->LinkEndChild(blogtext);//为blog节点添加文本值  
  19.   
  20.     TiXmlElement* websiteElenment=new TiXmlElement("website");  
  21.     infoElement->LinkEndChild(websiteElenment);  
  22.     TiXmlText* websitetext=new TiXmlText("http://www.fusijie.com");  
  23.     websiteElenment->LinkEndChild(websitetext);  
  24.   
  25.     myTinyXMLDoc->SaveFile(CCFileUtils::sharedFileUtils()->fullPathForFilename("testTiny.xml").c_str());//保存xml  
  26.   
  27.     delete myTinyXMLDoc;  
  28. }  
是不是很奇怪为什么new了那么多内存,但最后只delete TixmlDocument呢?其实在TinyXML中,子节点的内存管理都由父节点进行维护,所以不必在释放内存时对每个子节点都进行释放,而只需要释放父节点即可。

1.2.2.xml的读取

[cpp] view plaincopy
  1. void TestLayer::readTinyXML()  
  2. {  
  3.     TiXmlDocument* myTinyXMLDoc=new TiXmlDocument(CCFileUtils::sharedFileUtils()->fullPathForFilename("testTiny.xml").c_str());  
  4.     myTinyXMLDoc->LoadFile();  
  5.   
  6.     TiXmlElement* rootElement = myTinyXMLDoc->RootElement();  //获取根节点  
  7.     TiXmlElement* infoElement = rootElement->FirstChildElement();  // 获取Info节点    
  8.   
  9.     while (infoElement)   
  10.     {  
  11.   
  12.         TiXmlAttribute* attributeOfinfo = infoElement->FirstAttribute();  //获得info的属性    
  13.         while ( attributeOfinfo )   
  14.         {    
  15.             CCLOG("%s : %s",attributeOfinfo->Name(),attributeOfinfo->Value());//获取所有属性  
  16.             attributeOfinfo = attributeOfinfo->Next();    
  17.         }  
  18.   
  19.         TiXmlElement* blogElement = infoElement->FirstChildElement();//获得blog  
  20.         CCLOG("blog : %s",blogElement->GetText());    
  21.   
  22.         TiXmlElement* websiteElement = blogElement->NextSiblingElement();//获取website    
  23.         CCLOG("website : %s",websiteElement->GetText());    
  24.   
  25.         infoElement = infoElement->NextSiblingElement();//查找下一节点    
  26.     }    
  27.     delete myTinyXMLDoc;    
  28. }  

2.RapidXML


2.1.概况

RapidXML的主页是http://rapidxml.sourceforge.net/。在其手册中第四节comparison with others parsers可以看到在同等条件下它的解析效率是TinyXML的30到60倍,所以如果需要解析大文件的话,RapidXML是不二之选。


2.2.示例

首先下载RapidXML库,将四个hpp文件都加入cocos2d-x中,在包含头文件时,4个hpp都要进行包含。


2.2.1.xml的创建

[cpp] view plaincopy
  1. void TestLayer::writeRapidXML()  
  2. {  
  3.     rapidxml::xml_document<> myRapidXMLDoc;  
  4.   
  5.     rapidxml::xml_node<>* root = myRapidXMLDoc.allocate_node(rapidxml::node_element,"Jacky",NULL);//创建根节点  
  6.     myRapidXMLDoc.append_node(root);//追加根节点  
  7.   
  8.     rapidxml::xml_node<>* info = myRapidXMLDoc.allocate_node(rapidxml::node_element,"Info",NULL);//创建info节点  
  9.     info->append_attribute(myRapidXMLDoc.allocate_attribute("Name","Jacky"));  
  10.     info->append_attribute(myRapidXMLDoc.allocate_attribute("QQ","754505629"));  
  11.     info->append_attribute(myRapidXMLDoc.allocate_attribute("E-mail","Geek.Jacky@Gmail.com"));  
  12.     root->append_node(info);//追加info节点到root  
  13.   
  14.     //创建blog和website节点并追加到root  
  15.     rapidxml::xml_node<>* blog = myRapidXMLDoc.allocate_node(rapidxml::node_element,"blog","http://blog.csdn.net/jackystudio");  
  16.     info->append_node(blog);  
  17.     rapidxml::xml_node<>* website = myRapidXMLDoc.allocate_node(rapidxml::node_element,"website","http://www.fusijie.com");  
  18.     info->append_node(website);  
  19.       
  20.     //保存xml文档  
  21.     std::string text;  
  22.     rapidxml::print(std::back_inserter(text), myRapidXMLDoc, 0);  
  23.     CCLog(text.c_str());  
  24.     std::ofstream out(CCFileUtils::sharedFileUtils()->fullPathForFilename("testRapid.xml"));  
  25.     out << myRapidXMLDoc;  
  26. }  

2.2.2.xml的读取

[cpp] view plaincopy
  1. void TestLayer::readRapidXML()  
  2. {  
  3.     rapidxml::file<> fdoc(CCFileUtils::sharedFileUtils()->fullPathForFilename("testRapid.xml").c_str());//读取数据  
  4.     CCLog(fdoc.data());  
  5.     rapidxml::xml_document<> myRapidXMLDoc;  
  6.     myRapidXMLDoc.parse<0>(fdoc.data());//将数据写入xml_document  
  7.   
  8.     rapidxml::xml_node<>* root = myRapidXMLDoc.first_node();//获取根节点  
  9.   
  10.     rapidxml::xml_node<>* info = root->first_node();//获取Info节点  
  11.   
  12.     rapidxml::xml_attribute<>* info_attr = info->first_attribute();//获取info属性  
  13.     CCLog("%s:%s",info_attr->name(),info_attr->value());  
  14.     CCLog("%s:%s",info_attr->next_attribute()->name(),info_attr->next_attribute()->value());  
  15.     CCLog("%s:%s",info_attr->next_attribute()->next_attribute()->name(),info_attr->next_attribute()->next_attribute()->value());  
  16.   
  17.     rapidxml::xml_node<>* blog=info->first_node();//获取blog节点  
  18.     CCLog("%s:%s",blog->name(),blog->value());  
  19.   
  20.     rapidxml::xml_node<>* website=blog->next_sibling();//获取website节点  
  21.     CCLog("%s:%s",website->name(),website->value());  
  22. }  

3.生成xml的文件

以上2种方式生成的xml文件内容如下:

[html] view plaincopy
  1. <Jacky>  
  2.     <Info Name="Jacky" QQ="754505629" E-mail="Geek.Jacky@Gmail.com">  
  3.         <blog>http://blog.csdn.net/jackystudio</blog>  
  4.         <website>http://www.fusijie.com</website>  
  5.     </Info>  
  6. </Jacky>  

4.源码下载

下载地址:http://download.csdn.net/detail/jackyvincefu/6732361

8 2
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝流鼻涕2个月怎么办 一个月宝宝流鼻子怎么办 六个月宝宝流清水鼻涕怎么办 婴儿流鼻涕怎么办最简单方法 婴儿咳嗽流鼻涕怎么办最简单方法 6个月小孩流鼻涕怎么办 宝宝风寒感冒咳嗽流鼻涕怎么办 三个月宝宝流清鼻涕怎么办 三个月宝宝留清鼻涕怎么办 三个月婴儿流清鼻涕怎么办 宝宝7个月流鼻涕怎么办 6个月孩子流鼻涕怎么办 十一个月孩子感冒流鼻涕怎么办 7个月孩子流鼻涕怎么办 18个月的宝宝流鼻涕怎么办 6个月宝宝咳嗽流鼻涕怎么办 18个月宝宝咳嗽流鼻涕怎么办 6个月宝宝流鼻涕打喷嚏怎么办 宝宝18个月流鼻涕怎么办 18个月宝宝感冒流鼻涕怎么办 小孩睡觉鼻子不通气怎么办 宝宝鼻子里有鼻涕出不来怎么办 孩子喉咙有痰呼呼响怎么办 六个月宝宝鼻塞怎么办速效办法 两个月小孩鼻子不通气怎么办 一岁宝宝流清鼻涕怎么办 孩子鼻子里有鼻涕怎么办 宝宝晚上睡觉鼻子不通气怎么办 宝宝感冒睡觉鼻子不通气怎么办 宝宝3个月流鼻涕怎么办 小孩吃着了发烧怎么办 半岁宝宝鼻子塞怎么办 宝宝伤风鼻子不通气怎么办 二十天的宝宝伤风鼻子不通怎么办 一个多月的宝宝鼻子有鼻屎怎么办 2个月宝宝鼻子里有鼻屎怎么办 四个月婴儿感冒发烧怎么办 一个月婴儿感冒发烧怎么办 五个月婴儿感冒发烧怎么办 两个月的婴儿感冒发烧怎么办 9个月婴儿感冒发烧怎么办