加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2dx 3.1.1 用tinyxml2.h解释xml (C++)

发布时间:2020-12-14 21:08:45 所属栏目:百科 来源:网络整理
导读:cocos2dx 3.1.1怎样用tinyxml2.h解释xml? (C++) cocos2dx已经自带了tinyxml2用于xml的解释,很早之前从2.x的版本开始已经无需再特地去下载. 不过,tinyxm2关于3.x引擎的文档比较少,特此来贡献一个! 首先加入头文件: #include"cocos-ext.h” #include "tinyxml2

cocos2dx 3.1.1怎样用tinyxml2.h解释xml? (C++)

cocos2dx已经自带了tinyxml2用于xml的解释,很早之前从2.x的版本开始已经无需再特地去下载.

不过,tinyxm2关于3.x引擎的文档比较少,特此来贡献一个!


首先加入头文件:

#include"cocos-ext.h”

#include "tinyxml2/tinyxml2.h”

using namespace tinyxml2;

usingnamespacestd;



例子1:

text.xml文件内容如下

<?xml version="1.0"?>

<Hello>World</Hello>

xml解释:

[cpp] view plain copy
  1. stringfile_path=FileUtils::getInstance()->fullPathForFilename("testset.xml");//如果新建的是lua项目中需要写("res/text.xml");
  2. log("externalfilepath=%s",file_path.c_str());
  3. XMLDocumentdoc;
  4. //加载文件
  5. doc.LoadFile(file_path.c_str());
  6. constchar*content=doc.FirstChildElement("Hello")->GetText();
  7. log("Hello,%s",content);


输出结果Hello,World


例子2:

hello.xml文件内容

<?xml version="1.0"?>

<scenename="Depth">

<nodetype="camera">

<eye>0 10 10</eye>

<front>0 0 -1</front>

<refUp>0 1 0</refUp>

<fov>90</fov>

</node>

="Sphere">

<center>0 10 -10</center>

<radius>10</radius>

</node>

<nodetype="Plane">

<direction>0 10 -10</direction>

<distance>10</distance>

</scene>


xml解析:

[cpp] view plain copy
  1. stringfile_path=FileUtils::getInstance()->fullPathForFilename("hello.xml");
  2. XMLDocumentdocument;
  3. document.LoadFile(file_path.c_str());
  4. XMLElement*scene=document.RootElement();
  5. XMLElement*surface=scene->FirstChildElement("node");
  6. while(surface)
  7. {
  8. XMLElement*surfaceChild=surface->FirstChildElement();
  9. char*content;
  10. constXMLAttribute*attributeOfSurface=surface->FirstAttribute();
  11. log("%s:%s",attributeOfSurface->Name(),attributeOfSurface->Value());
  12. while(surfaceChild)
  13. {
  14. content=surfaceChild->GetText();
  15. surfaceChild=surfaceChild->NextSiblingElement();
  16. log("%s",content);
  17. }
  18. surface=surface->NextSiblingElement();
  19. }


输出结果:

cocos2d: type:camera

cocos2d: 0 10 10

cocos2d: 0 0 -1

cocos2d: 0 1 0

cocos2d: 90

cocos2d: type:Sphere

cocos2d: 0 10 -10

cocos2d: 10

cocos2d: type:Plane

cocos2d: 0 10 -10

cocos2d: 10


参考资料:

http://blog.csdn.net/educast/article/details/12908455

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读