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

前端之家收集整理的这篇文章主要介绍了cocos2dx 3.1.1 用tinyxml2.h解释xml (C++)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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


@H_403_2@

首先加入头文件:

#include"cocos-ext.h”

#include "tinyxml2/tinyxml2.h”

using namespace tinyxml2;

using@H_403_2@namespace@H_403_2@std@H_403_2@;@H_403_2@



例子1:

text.xml文件内容如下

<?xml version="1.0"?>

<Hello>World</Hello>

xml解释:

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


输出结果Hello,World


例子2:

hello.xml文件内容

<?xml version="1.0"@H_403_2@?>

<scenename@H_403_2@="Depth"@H_403_2@>

@H_403_2@<node@H_403_2@type@H_403_2@=@H_403_2@"camera">@H_403_2@

<eye>@H_403_2@0 10 10</eye>@H_403_2@

@H_403_2@<front>0 0 -1@H_403_2@</front>

@H_403_2@<refUp>0 1 0@H_403_2@</refUp>

@H_403_2@<fov>90@H_403_2@</fov>

@H_403_2@</node>

=@H_403_2@"Sphere">@H_403_2@

@H_403_2@<center>0 10 -10@H_403_2@</center>

@H_403_2@<radius>10@H_403_2@</radius>

@H_403_2@</node>

@H_403_2@<nodetype@H_403_2@="Plane"@H_403_2@>

@H_403_2@<direction>0 10 -10@H_403_2@</direction>

@H_403_2@<distance>10@H_403_2@</distance>

</scene>


xml解析:

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


输出结果:

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

猜你在找的Cocos2d-x相关文章