C++库(TinyXML)

C++库(TinyXML)

什么是XML?

  “当 XML(扩展标记语言)于 1998 年 2 月被引入软件工业界时,它给整个行业带来了一场风暴。有史以来第一次,这个世界拥有了一种用来结构化文档和数据的通用且适应性强的格式,它不仅仅可以用于 WEB,而且可以被用于任何地方。”

  XML 指扩展标记语言,是一种信息交换的标准。

<!-- a xml simple example-->
<Persons>
<Person ID="1">
<name>Kobe</name>
<age>24</age>
</Person>
<Person ID="2">
<name>Michael</name>
<age>23</age>
</Person>
</Persons>

什么是TinyXML?

  当前,对xml的使用非常广泛,读取和设置xml配置文件是我们最常用的操作。常见C/C++ XML解析器有Tinyxml、XERCES、squashxml、xmlite、pugxml、libxml等等,这些解析器有些是支持多语言的,有些只是单纯C/C++的。

  TinyXML是目前非常流行的一款基于DOM模型的XML解析器,简单易用且小巧玲珑,非常适合存储简单数据,配置文件,对象序列化等数据量不是很大的操作。这个解析库的模型通过解析XML文件,然后在内存中生成DOM模型,从而让我们很方便的遍历这棵XML树。

下载安装TinyXML

  下载https://sourceforge.net/projects/tinyxml/

      安装:解压缩tinyXML后,将这六个文件添加到你的C++工程中,分别是tinystr.h、tinystr.cpp、tinyxml.h、tinyxml.cpp、tinyxmlerror.cpp、tinyxmlparser.cpp。在需要操作xml文件的地方,使用如下代码,就可以引入TinyXML类库。

#include<tinyxml>   或 #include "tinyxml.h"

TinyXML类结构

  TiXmlBase:整个TinyXML模型的基类。

  TiXmlAttribute:对应于XML中的元素的属性。

  TiXmlNode:对应于DOM结构中的节点。

  TiXmlComment:对应于XML中的注释

  TiXmlDeclaration:对应于XML中的申明部分,即<?versiong="1.0" ?>。

  TiXmlDocument:对应于XML的整个文档。

  TiXmlElement:对应于XML的元素。

  TiXmlText:对应于XML的文字部分

  TiXmlUnknown:对应于XML的未知部分。

  TiXmlHandler:定义了针对XML的一些操作。

 

一个XML读写例子

 #include <iostream>
#include "tinyxml.h"
#include "tinystr.h"
#include <string>
#include <windows.h>
#include <atlstr.h> using namespace std; CString GetAppPath()
{
TCHAR modulePath[MAX_PATH];
GetModuleFileName(NULL, modulePath, MAX_PATH);
CString strModulePath(modulePath);
strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\')));
return strModulePath;
} bool CreateXmlFile(string& szFileName)
{
try
{
TiXmlDocument *myDocument = new TiXmlDocument();
TiXmlElement *RootElement = new TiXmlElement("Persons");
myDocument->LinkEndChild(RootElement);
TiXmlElement *PersonElement = new TiXmlElement("Person");
RootElement->LinkEndChild(PersonElement);
PersonElement->SetAttribute("ID", "");
TiXmlElement *NameElement = new TiXmlElement("name");
TiXmlElement *AgeElement = new TiXmlElement("age");
PersonElement->LinkEndChild(NameElement);
PersonElement->LinkEndChild(AgeElement);
TiXmlText *NameContent = new TiXmlText("Michael");
TiXmlText *AgeContent = new TiXmlText("");
NameElement->LinkEndChild(NameContent);
AgeElement->LinkEndChild(AgeContent);
CString appPath = GetAppPath();
string seperator = "\\";
string fullPath = appPath.GetBuffer() +seperator+szFileName;
myDocument->SaveFile(fullPath.c_str());
}
catch (string& e)
{
return false;
}
return true;
} bool ReadXmlFile(string& szFileName)
{
try
{
CString appPath = GetAppPath();
string seperator = "\\";
string fullPath = appPath.GetBuffer() +seperator+szFileName;
TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str());
myDocument->LoadFile();
TiXmlElement *RootElement = myDocument->RootElement();
cout << RootElement->Value() << endl;
TiXmlElement *FirstPerson = RootElement->FirstChildElement();
TiXmlElement *NameElement = FirstPerson->FirstChildElement();
TiXmlElement *AgeElement = NameElement->NextSiblingElement();
TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute();
cout << NameElement->FirstChild()->Value() << endl;
cout << AgeElement->FirstChild()->Value() << endl;
cout << IDAttribute->Value()<< endl;
}
catch (string& e)
{
return false;
}
return true;
} int main(void)
{
string fileName = "info.xml";
CreateXmlFile(fileName);
ReadXmlFile(fileName);
}
上一篇:mysql数据库优化(转)


下一篇:php缓存模块apc可能导致php-fpm终止