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

【玩转cocos2d-x之三十二】xml的解析,第1张

概述原创作品,转载请标明:http://www.voidcn.com/article/p-dpestbvf-ep.html cocos2d-x中对xml的解析是采用的TinyXML库,而对plist的解析同时结合了CCDictionary来处理,这里简单介绍下cocos2d-x中解析xml的两种方式,也是常用的xml两个C++解析库:TinyXML和RapidXML。xml被设计用于数据存储和传输,重

原创作品,转载请标明http://www.jb51.cc/article/p-dpestbvf-ep.html


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


1.TinyXML


1.1.概况@H_403_39@

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


1.2.示例@H_403_39@

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


@L_403_6@1.2.1.xml的创建

[cpp] view plain copy voIDTestLayer::writeTinyXML() { tixmldocument*myTinyXMLDoc=newtixmldocument();//创建文档对象 tixmlElement*rootElement=newtixmlElement("Jacky");//创建根节点并连接到文档 myTinyXMLDoc->linkEndChild(rootElement); tixmlElement*infoElement=newtixmlElement("Info");//创建info节点并连接到根节点 rootElement->linkEndChild(infoElement); infoElement->SetAttribute("name","Jacky");//为info节点设置属性 infoElement->SetAttribute("QQ","754505629"); infoElement->SetAttribute("E-mail","[email protected]"); tixmlElement*blogElement=newtixmlElement("blog"); infoElement->linkEndChild(blogElement);//创建blog节点并连接到info节点 tixmlText*blogtext=newtixmlText("http://blog.csdn.net/jackystudio"); blogElement->linkEndChild(blogtext);//为blog节点添加文本值 tixmlElement*websiteElenment=newtixmlElement("website"); infoElement->linkEndChild(websiteElenment); tixmlText*websitetext=newtixmlText("http://www.fusijIE.com"); websiteElenment->linkEndChild(websitetext); myTinyXMLDoc->Savefile(CCfileUtils::sharedfileUtils()->fullPathForfilename("testTiny.xml").c_str());//保存xml deletemyTinyXMLDoc; } 是不是很奇怪为什么new了那么多内存,但最后只delete tixmldocument呢?其实在TinyXML中,子节点的内存管理都由父节点进行维护,所以不必在释放内存时对每个子节点都进行释放,而只需要释放父节点即可。
1.2.2.xml的读取

copy voIDTestLayer::readTinyXML() newtixmldocument(CCfileUtils::sharedfileUtils()->fullPathForfilename("testTiny.xml").c_str()); myTinyXMLDoc->Loadfile(); tixmlElement*rootElement=myTinyXMLDoc->RootElement();//获取根节点 tixmlElement*infoElement=rootElement->FirstChildElement();//获取Info节点 while(infoElement) tixmlAttribute*attributeOfinfo=infoElement->FirstAttribute();//获得info的属性 while(attributeOfinfo) cclOG("%s:%s",attributeOfinfo->name(),attributeOfinfo->Value());//获取所有属性 attributeOfinfo=attributeOfinfo->Next(); } tixmlElement*blogElement=infoElement->FirstChildElement();//获得blog cclOG("blog:%s",blogElement->GetText()); tixmlElement*websiteElement=blogElement->NextSiblingElement();//获取website cclOG("website:%s",websiteElement->GetText()); infoElement=infoElement->NextSiblingElement();//查找下一节点 } }
2.RAPIdXML


2.1.概况@H_403_39@

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


2.2.示例@H_403_39@

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


2.2.1.xml的创建

copy voIDTestLayer::writerapIDXML() rAPIdxml::xml_document<>myRAPIdXMLDoc; rAPIdxml::xml_node<>*root=myRAPIdXMLDoc.allocate_node(rAPIdxml::node_element,"Jacky",NulL);//创建根节点 myRAPIdXMLDoc.append_node(root);//追加根节点 rAPIdxml::xml_node<>*info=myRAPIdXMLDoc.allocate_node(rAPIdxml::node_element,"Info",0); background-color:inherit">//创建info节点 info->append_attribute(myRAPIdXMLDoc.allocate_attribute("name","Jacky")); info->append_attribute(myRAPIdXMLDoc.allocate_attribute("QQ","754505629")); info->append_attribute(myRAPIdXMLDoc.allocate_attribute("E-mail","[email protected]")); root->append_node(info);//追加info节点到root //创建blog和website节点并追加到root rAPIdxml::xml_node<>*blog=myRAPIdXMLDoc.allocate_node(rAPIdxml::node_element,"blog","http://blog.csdn.net/jackystudio"); info->append_node(blog); rAPIdxml::xml_node<>*website=myRAPIdXMLDoc.allocate_node(rAPIdxml::node_element,"website","http://www.fusijIE.com"); info->append_node(website); //保存xml文档 std::stringtext; rAPIdxml::print(std::back_inserter(text),myRAPIdXMLDoc,0); cclog(text.c_str()); std::ofstreamout(CCfileUtils::sharedfileUtils()->fullPathForfilename("testRAPId.xml")); out<<myRAPIdXMLDoc; }
2.2.2.xml的读取

copy voIDTestLayer::readRAPIdXML() rAPIdxml::file<>fdoc(CCfileUtils::sharedfileUtils()->fullPathForfilename("testRAPId.xml").c_str());//读取数据 cclog(fdoc.data()); myRAPIdXMLDoc.parse<0>(fdoc.data());//将数据写入xml_document rAPIdxml::xml_node<>*root=myRAPIdXMLDoc.first_node(); rAPIdxml::xml_node<>*info=root->first_node();//获取Info节点 rAPIdxml::xml_attribute<>*info_attr=info->first_attribute();//获取info属性 cclog("%s:%s",info_attr->name(),info_attr->value()); cclog("%s:%s",info_attr->next_attribute()->name(),info_attr->next_attribute()->value()); rAPIdxml::xml_node<>*blog=info->first_node();//获取blog节点 rAPIdxml::xml_node<>*website=blog->next_sibling();//获取website节点 }
3.生成xml的文件

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

[HTML] copy <Jacky> Infoname="Jacky"QQ="754505629"E-mail="[email protected]"> blog>http://blog.csdn.net/jackystudio</website>http://www.fusijIE.comInfo>
4.源码下载

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

总结

以上是内存溢出为你收集整理的【玩转cocos2d-x之三十二】xml的解析全部内容,希望文章能够帮你解决【玩转cocos2d-x之三十二】xml的解析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1065416.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存