转自:http://blog.youkuaiyun.com/tennysonsky/article/details/48630005#comments
TinyXML是一个开源的解析XML的解析库,能够用于C++,能够在Windows或Linux中编译。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。
DOM模型即文档对象模型,是将整个文档分成多个元素(如书、章、节、段等),并利用树型结构表示这些元素之间的顺序关系以及嵌套包含关系。
使用之前,需要先下载TinyXML类库:http://download.youkuaiyun.com/detail/overlord_bingo/9789750
然后解压缩TinyXML后,将后六个文件添加到你的c++工程中,分别是main.cpp、tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。
如本示例中,只有 main.cpp 才是测试代码;
编写代码时,只需要包含 tinyxml.h 头文件即可,但是,编译时却需要把所有.cpp 文件都加上。
在VS中编译时出现:error C1010: 在查找预编译头时遇到意外的文件结尾。是否忘记了向源中添加“#include "stdafx.h"”?
示例代码如下:
#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()<<" ";
TiXmlAttribute *pattr = RootElement->FirstAttribute();//第一个属性
while (NULL != pattr) //输出所有属性
{
cout << pattr->Name() << ":" << pattr->Value() << " ";
pattr = pattr->Next();
}
cout << endl;
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!= NULL;
sonElement = sonElement->NextSiblingElement())
{
cout << sonElement->Value() <<":"<< sonElement->FirstChild()->Value() << endl;
}
}
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
writeXmlFile();
readXmlFile();
system("pause");
return 0;
}
编译运行结果如下:
生成的xml文件内容如下: