转自:http://blog.youkuaiyun.com/tennysonsky/article/details/48630005
原作者:Mike__Jiang
TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
使用之前,需要先下载TinyXML类库:http://download.youkuaiyun.com/detail/tennysonsky。也可在sourceforge上下载:http://sourceforge.net/projects/tinyxml/。
然后解压缩TinyXML后,将这六个文件添加到你的c++工程中,分别是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。
如本示例中,只有 main.cpp 才是测试代码:
编写代码时,只需要包含 tinyxml.h 头文件即可,但是,编译时却需要把所有.cpp 文件都加上。
示例代码如下:
- #include <stdio.h>
- #include "tinyxml.h"
- #include <iostream>
- #include <cstring>
- using namespace std;
- /*
- TiXmlDocument:文档类,它代表了整个xml文件
- TiXmlDeclaration:声明类,它表示文件的声明部分
- TiXmlComment:注释类,它表示文件的注释部分
- TiXmlElement:元素类,它是文件的主要部分,并且支持嵌套结构,一般使用这种结构来分类的存储信息,它可以包含属性类和文本类
- TiXmlAttribute/TiXmlAttributeSet:元素属性,它一般嵌套在元素中,用于记录此元素的一些属性
- TiXmlText:文本对象,它嵌套在某个元素内部
- */
- //创建xml文件
- int writeXmlFile()
- {
- TiXmlDocument *writeDoc = new TiXmlDocument; //xml文档指针
- //文档格式声明
- TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
- writeDoc->LinkEndChild(decl); //写入文档
- int n = 3; //父节点个数
- TiXmlElement *RootElement = new TiXmlElement("Info");//根元素
- RootElement->SetAttribute("num", n); //属性
- writeDoc->LinkEndChild(RootElement);
- for(int i=0; i<n; i++)//n个父节点
- {
- TiXmlElement *StuElement = new TiXmlElement("Stu");//Stu
- //设置属性
- StuElement->SetAttribute("class","A");
- if(2 == i)
- {
- StuElement->SetAttribute("class","B");
- }
- StuElement->SetAttribute("id",i+1);
- StuElement->SetAttribute("flag", (i+1)*10);
- RootElement->LinkEndChild(StuElement);//父节点写入文档
- //姓名
- TiXmlElement *nameElement = new TiXmlElement("name");
- StuElement->LinkEndChild(nameElement);
- TiXmlText *nameContent = new TiXmlText("mike");
- nameElement->LinkEndChild(nameContent);
- //分数
- TiXmlElement *scoreElement = new TiXmlElement("score");
- StuElement->LinkEndChild(scoreElement);
- TiXmlText *scoreContent = new TiXmlText("88");
- scoreElement->LinkEndChild(scoreContent);
- //城市
- TiXmlElement *cityElement = new TiXmlElement("city");
- StuElement->LinkEndChild(cityElement);
- TiXmlText *cityContent = new TiXmlText("Shenzhen");
- cityElement->LinkEndChild(cityContent);
- }
- writeDoc->SaveFile("stu_info.xml");
- delete writeDoc;
- return 1;
- }
- //解析xml文件
- int readXmlFile()
- {
- TiXmlDocument mydoc("stu_info.xml");//xml文档对象
- bool loadOk=mydoc.LoadFile();//加载文档
- if(!loadOk)
- {
- cout<<"could not load the test file.Error:"<<mydoc.ErrorDesc()<<endl;
- exit(1);
- }
- TiXmlElement *RootElement=mydoc.RootElement(); //根元素, Info
- cout<< "[root name]" << RootElement->Value() <<"\n";
- TiXmlElement *pEle=RootElement;
- //遍历该结点
- for(TiXmlElement *StuElement = pEle->FirstChildElement();//第一个子元素
- StuElement != NULL;
- StuElement = StuElement->NextSiblingElement())//下一个兄弟元素
- {
- // StuElement->Value() 节点名称
- cout<< StuElement->Value() <<" ";
- TiXmlAttribute *pAttr=StuElement->FirstAttribute();//第一个属性
- while( NULL != pAttr) //输出所有属性
- {
- cout<<pAttr->Name()<<":"<<pAttr->Value()<<" ";
- pAttr=pAttr->Next();
- }
- cout<<endl;
- //输出子元素的值
- for(TiXmlElement *sonElement=StuElement->FirstChildElement();
- sonElement;
- sonElement=sonElement->NextSiblingElement())
- {
- cout<<sonElement->FirstChild()->Value()<<endl;
- }
- }
- return 1;
- }
- int main(int argc, char *argv[])
- {
- writeXmlFile();
- printf("\nafter write\n");
- readXmlFile();
- return 0;
- }
#include <stdio.h>
#include "tinyxml.h"
#include <iostream>
#include <cstring>
using namespace std;
/*
TiXmlDocument:文档类,它代表了整个xml文件
TiXmlDeclaration:声明类,它表示文件的声明部分
TiXmlComment:注释类,它表示文件的注释部分
TiXmlElement:元素类,它是文件的主要部分,并且支持嵌套结构,一般使用这种结构来分类的存储信息,它可以包含属性类和文本类
TiXmlAttribute/TiXmlAttributeSet:元素属性,它一般嵌套在元素中,用于记录此元素的一些属性
TiXmlText:文本对象,它嵌套在某个元素内部
*/
//创建xml文件
int writeXmlFile()
{
TiXmlDocument *writeDoc = new TiXmlDocument; //xml文档指针
//文档格式声明
TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "UTF-8", "yes");
writeDoc->LinkEndChild(decl); //写入文档
int n = 3; //父节点个数
TiXmlElement *RootElement = new TiXmlElement("Info");//根元素
RootElement->SetAttribute("num", n); //属性
writeDoc->LinkEndChild(RootElement);
for(int i=0; i<n; i++)//n个父节点
{
TiXmlElement *StuElement = new TiXmlElement("Stu");//Stu
//设置属性
StuElement->SetAttribute("class","A");
if(2 == i)
{
StuElement->SetAttribute("class","B");
}
StuElement->SetAttribute("id",i+1);
StuElement->SetAttribute("flag", (i+1)*10);
RootElement->LinkEndChild(StuElement);//父节点写入文档
//姓名
TiXmlElement *nameElement = new TiXmlElement("name");
StuElement->LinkEndChild(nameElement);
TiXmlText *nameContent = new TiXmlText("mike");
nameElement->LinkEndChild(nameContent);
//分数
TiXmlElement *scoreElement = new TiXmlElement("score");
StuElement->LinkEndChild(scoreElement);
TiXmlText *scoreContent = new TiXmlText("88");
scoreElement->LinkEndChild(scoreContent);
//城市
TiXmlElement *cityElement = new TiXmlElement("city");
StuElement->LinkEndChild(cityElement);
TiXmlText *cityContent = new TiXmlText("Shenzhen");
cityElement->LinkEndChild(cityContent);
}
writeDoc->SaveFile("stu_info.xml");
delete writeDoc;
return 1;
}
//解析xml文件
int readXmlFile()
{
TiXmlDocument mydoc("stu_info.xml");//xml文档对象
bool loadOk=mydoc.LoadFile();//加载文档
if(!loadOk)
{
cout<<"could not load the test file.Error:"<<mydoc.ErrorDesc()<<endl;
exit(1);
}
TiXmlElement *RootElement=mydoc.RootElement(); //根元素, Info
cout<< "[root name]" << RootElement->Value() <<"\n";
TiXmlElement *pEle=RootElement;
//遍历该结点
for(TiXmlElement *StuElement = pEle->FirstChildElement();//第一个子元素
StuElement != NULL;
StuElement = StuElement->NextSiblingElement())//下一个兄弟元素
{
// StuElement->Value() 节点名称
cout<< StuElement->Value() <<" ";
TiXmlAttribute *pAttr=StuElement->FirstAttribute();//第一个属性
while( NULL != pAttr) //输出所有属性
{
cout<<pAttr->Name()<<":"<<pAttr->Value()<<" ";
pAttr=pAttr->Next();
}
cout<<endl;
//输出子元素的值
for(TiXmlElement *sonElement=StuElement->FirstChildElement();
sonElement;
sonElement=sonElement->NextSiblingElement())
{
cout<<sonElement->FirstChild()->Value()<<endl;
}
}
return 1;
}
int main(int argc, char *argv[])
{
writeXmlFile();
printf("\nafter write\n");
readXmlFile();
return 0;
}
编译运行结果如下:
生成的xml文件内容如下:
- <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <Info num="3">
- <Stu class="A" id="1" flag="10">
- <name>mike</name>
- <score>88</score>
- <city>Shenzhen</city>
- </Stu>
- <Stu class="A" id="2" flag="20">
- <name>mike</name>
- <score>88</score>
- <city>Shenzhen</city>
- </Stu>
- <Stu class="B" id="3" flag="30">
- <name>mike</name>
- <score>88</score>
- <city>Shenzhen</city>
- </Stu>
- </Info>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Info num="3">
<Stu class="A" id="1" flag="10">
<name>mike</name>
<score>88</score>
<city>Shenzhen</city>
</Stu>
<Stu class="A" id="2" flag="20">
<name>mike</name>
<score>88</score>
<city>Shenzhen</city>
</Stu>
<Stu class="B" id="3" flag="30">
<name>mike</name>
<score>88</score>
<city>Shenzhen</city>
</Stu>
</Info>
本教程示例代码下载请点此链接:http://download.youkuaiyun.com/detail/tennysonsky。