cocos2dx 3.1.1 用tinyxml2.h解释xml (C++)
2014-08-26 13:52
666 查看
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;
using namespace
std;
例子1:
text.xml文件内容如下
<?xml version="1.0"?>
<Hello>World</Hello>
xml解释:
输出结果Hello,World
例子2:
hello.xml文件内容
<?xml version="1.0"?>
<scene name="Depth">
<node type="camera">
<eye>0 10 10</eye>
<front>0 0 -1</front>
<refUp>0 1 0</refUp>
<fov>90</fov>
</node>
<node type="Sphere">
<center>0 10 -10</center>
<radius>10</radius>
</node>
<node type="Plane">
<direction>0 10 -10</direction>
<distance>10</distance>
</node>
</scene>
xml解析:
输出结果:
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
cocos2dx已经自带了tinyxml2用于xml的解释,很早之前从2.x的版本开始已经无需再特地去下载.
不过,tinyxm2关于3.x引擎的文档比较少,特此来贡献一个!
首先加入头文件:
#include "cocos-ext.h”
#include "tinyxml2/tinyxml2.h”
using namespace tinyxml2;
using namespace
std;
例子1:
text.xml文件内容如下
<?xml version="1.0"?>
<Hello>World</Hello>
xml解释:
string file_path = FileUtils::getInstance()->fullPathForFilename("testset.xml");//如果新建的是lua项目中需要写("res/text.xml"); log("external file path = %s",file_path.c_str()); XMLDocument doc; //加载文件 doc.LoadFile(file_path.c_str()); const char* content= doc.FirstChildElement( "Hello" )->GetText(); log( "Hello,%s", content );
输出结果Hello,World
例子2:
hello.xml文件内容
<?xml version="1.0"?>
<scene name="Depth">
<node type="camera">
<eye>0 10 10</eye>
<front>0 0 -1</front>
<refUp>0 1 0</refUp>
<fov>90</fov>
</node>
<node type="Sphere">
<center>0 10 -10</center>
<radius>10</radius>
</node>
<node type="Plane">
<direction>0 10 -10</direction>
<distance>10</distance>
</node>
</scene>
xml解析:
string file_path = FileUtils::getInstance()->fullPathForFilename("hello.xml"); log("external file path = %s",file_path.c_str()); XMLDocument document; document.LoadFile(file_path.c_str()); XMLElement *scene=document.RootElement(); XMLElement *surface=scene->FirstChildElement("node"); while (surface) { XMLElement *surfaceChild=surface->FirstChildElement(); const char* content; const XMLAttribute *attributeOfSurface = surface->FirstAttribute(); log("%s:%s",attributeOfSurface->Name(),attributeOfSurface->Value()); while(surfaceChild) { content=surfaceChild->GetText(); surfaceChild=surfaceChild->NextSiblingElement(); log("%s",content); } surface=surface->NextSiblingElement(); }
输出结果:
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
相关文章推荐
- cocos2dx 3.1.1 用tinyxml2.h解释xml (C++)
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- C++使用TinyXml2解析Xml
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- 《转》 在C++中使用TinyXML2解析xml
- cocos2dx tinyxml读写示例,tinyxml2读xml示例,
- cocos2dx 3.1.1 ProgressTimer游戏进度条简明实例(语言C++)
- cocos2dx 3.1.1 mac上新建项目lua C++
- cocos2dx C++使用rapidxml读取XML配置文件
- C++ XML 解释库
- 在C++中使用TinyXML2解析xml
- C++使用tinyxml创建和解析xml文件(本文用于cocos2dx)
- c++用tinyxml2读xml文件,转化为字节码,解析并保存
- 在C++中使用TinyXML2解析xml
- 《转》 在C++中使用TinyXML2解析xml
- cocos2dx3.1.1 code ide LUA项目调用C++
- 在C++中使用TinyXML2解析xml(2)
- 在C++中使用TinyXML2解析xml