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:
<?xml version="1.0"?>
<Hello>World</Hello>
xml解释:
- 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@
- log("externalfilepath=%s"@H_403_2@,file_path.c_str());@H_403_2@@H_403_2@
- XMLDocumentdoc;@H_403_2@
- //加载文件@H_403_2@@H_403_2@@H_403_2@
- doc.LoadFile(file_path.c_str());@H_403_2@
- 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@
- log("Hello,%s"@H_403_2@,content);@H_403_2@@H_403_2@
输出结果Hello,World
例子2:
<?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@<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解析:
- stringfile_path=FileUtils::getInstance()->fullPathForFilename(@H_403_2@"hello.xml"@H_403_2@);@H_403_2@@H_403_2@
- 403_2@@H_403_2@
- @H_403_2@
- @H_403_2@
- XMLDocumentdocument;@H_403_2@
- document.LoadFile(file_path.c_str());@H_403_2@
- XMLElement*scene=document.RootElement();@H_403_2@
- XMLElement*surface=scene->FirstChildElement("node"@H_403_2@);@H_403_2@@H_403_2@
- while@H_403_2@(surface)@H_403_2@@H_403_2@
- {@H_403_2@
- XMLElement*surfaceChild=surface->FirstChildElement();@H_403_2@
- char@H_403_2@*content;@H_403_2@@H_403_2@
- const@H_403_2@XMLAttribute*attributeOfSurface=surface->FirstAttribute();@H_403_2@@H_403_2@
- log("%s:%s"@H_403_2@,attributeOfSurface->Name(),attributeOfSurface->Value());@H_403_2@@H_403_2@
- while@H_403_2@(surfaceChild)@H_403_2@@H_403_2@
- {@H_403_2@
- content=surfaceChild->GetText();@H_403_2@
- surfaceChild=surfaceChild->NextSiblingElement();@H_403_2@
- log("%s"@H_403_2@,content);@H_403_2@@H_403_2@
- }@H_403_2@
- surface=surface->NextSiblingElement();@H_403_2@
- }@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