《转》 在C++中使用TinyXML2解析xml
2017-05-08 10:11
435 查看
读取和设置xml配置文件是最经常使用的操作,试用了几个C++的XML解析器,个人感觉TinyXML是使用起来最舒服的,由于它的API接口和Java的十分类似。面向对象性非常好。
TinyXML是一个开源的解析XML的解析库,可以用于C++,可以在Windows或Linux中编译。这个解析库的模型通过解析XML文件。然后在内存中生成DOM模型。从而让我们非常方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包括关系。
只是官方的文档并非非常完好。样例更是不知所云...然后就有了以下的内容。
这里用的是TinyXML2,相比于TinyXML1,它更小,更轻量,内存的使用也更加有效。
1.配置TinyXML2
去这里把项目弄下来。然后解压,我们之须要里面的tinyxml2.h和tinyxml2.cpp,将他们拷到project文件夹里面。
2.HelloWorld
在项目中创建test.xml,内容例如以下:
[html] view
plaincopy
<?xml version="1.0"?>
<Hello>World</Hello>
创建main.cpp
[cpp] view
plaincopy
#include <iostream>
#include"tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void example1()
{
XMLDocument doc;
doc.LoadFile("test.xml");
const char* content= doc.FirstChildElement( "Hello" )->GetText();
printf( "Hello,%s", content );
}
int main()
{
example1();
return 0;
}
编译执行:
3.略微复杂一些的样例
以下这个样例的场景更可能在project中遇到,就是在XML中存储一些数据。然后由程序来调用。
[html] view
plaincopy
<?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>
[cpp] view
plaincopy
#include <iostream>
#include"tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void example2()
{
XMLDocument doc;
doc.LoadFile("test.xml");
XMLElement *scene=doc.RootElement();
XMLElement *surface=scene->FirstChildElement("node");
while (surface)
{
XMLElement *surfaceChild=surface->FirstChildElement();
const char* content;
const XMLAttribute *attributeOfSurface = surface->FirstAttribute();
cout<< attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << endl;
while(surfaceChild)
{
content=surfaceChild->GetText();
surfaceChild=surfaceChild->NextSiblingElement();
cout<<content<<endl;
}
surface=surface->NextSiblingElement();
}
}
int main()
{
example1();
return 0;
}
执行结果
解释一下几个函数:
FirstChildElement(const char* value=0):获取第一个值为value的子节点。value默认值为空,则返回第一个子节点。
RootElement():获取根节点,相当于FirstChildElement的空參数版本号。
const XMLAttribute* FirstAttribute() const:获取第一个属性值。
XMLHandle NextSiblingElement( const char* _value=0 ) :获得下一个节点。
TinyXML是一个开源的解析XML的解析库,可以用于C++,可以在Windows或Linux中编译。这个解析库的模型通过解析XML文件。然后在内存中生成DOM模型。从而让我们非常方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包括关系。
只是官方的文档并非非常完好。样例更是不知所云...然后就有了以下的内容。
这里用的是TinyXML2,相比于TinyXML1,它更小,更轻量,内存的使用也更加有效。
1.配置TinyXML2
去这里把项目弄下来。然后解压,我们之须要里面的tinyxml2.h和tinyxml2.cpp,将他们拷到project文件夹里面。
2.HelloWorld
在项目中创建test.xml,内容例如以下:
[html] view
plaincopy
<?xml version="1.0"?>
<Hello>World</Hello>
创建main.cpp
[cpp] view
plaincopy
#include <iostream>
#include"tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void example1()
{
XMLDocument doc;
doc.LoadFile("test.xml");
const char* content= doc.FirstChildElement( "Hello" )->GetText();
printf( "Hello,%s", content );
}
int main()
{
example1();
return 0;
}
编译执行:
3.略微复杂一些的样例
以下这个样例的场景更可能在project中遇到,就是在XML中存储一些数据。然后由程序来调用。
[html] view
plaincopy
<?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>
[cpp] view
plaincopy
#include <iostream>
#include"tinyxml2.h"
using namespace std;
using namespace tinyxml2;
void example2()
{
XMLDocument doc;
doc.LoadFile("test.xml");
XMLElement *scene=doc.RootElement();
XMLElement *surface=scene->FirstChildElement("node");
while (surface)
{
XMLElement *surfaceChild=surface->FirstChildElement();
const char* content;
const XMLAttribute *attributeOfSurface = surface->FirstAttribute();
cout<< attributeOfSurface->Name() << ":" << attributeOfSurface->Value() << endl;
while(surfaceChild)
{
content=surfaceChild->GetText();
surfaceChild=surfaceChild->NextSiblingElement();
cout<<content<<endl;
}
surface=surface->NextSiblingElement();
}
}
int main()
{
example1();
return 0;
}
执行结果
解释一下几个函数:
FirstChildElement(const char* value=0):获取第一个值为value的子节点。value默认值为空,则返回第一个子节点。
RootElement():获取根节点,相当于FirstChildElement的空參数版本号。
const XMLAttribute* FirstAttribute() const:获取第一个属性值。
XMLHandle NextSiblingElement( const char* _value=0 ) :获得下一个节点。
相关文章推荐
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- C++使用TinyXml2解析Xml
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml(2)
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- 《转》 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- 在C++中使用TinyXML2解析xml
- 【类库解析】一个轻量级C++ xml解释器 TinyXML的使用方法
- cocos2d-x3.10 使用tinyxml2在Android配合FileUtilsAndroid解析XML文件
- c++使用tinyXml进行XML解析
- XML解析简介及Xerces-C++简单使用举例
- C++中使用Expat解析XML
- C++中使用Expat解析XML
- cocos2d-x3.10 使用tinyxml2在Android配合FileUtilsAndroid解析XML文件