对于xml文件,目前我的工作只是集中在配置文件和作为简单的信息文件来用,因此我不太喜欢使用msxml这种重量级的xml解析器,特别是使用msxml解析xml涉及到复杂的com类型转换,更是令人感觉繁琐。因此对于简单的xml文件的解析,我更愿意使用开源的TinyXml。
首先介绍一下TinyXml吧。TinyXML是目前非常流行的一款基于DOM模型的XML解析器,简单易用且小巧玲珑,非常适合存储简单数据,配置文件,对象序列化等数据量不是很大的操作,其主页是:http://www.grinninglizard.com/tinyxml/ ,目前最新版本是2.5.3 版本。
TinyXml网上的教程很多,但是我觉得写得都不怎样(感觉就是看完之后就没学会)。没办法,只得自己整理一篇适合自己的,至于适不适合别人,就见仁见智了。我感觉xml文件本质就是小型的数据库,换个角度来说就是,你对数据库有什么操作你对xml文件就应能实现什么操作。一般而言,对数据库的操作包括以下几种:新建数据库、查询数据库、修改数据库和删除数据库。那么对应xml文件就是新建xml文件、查询xml文件的指定节点的值,修改xml文件中节点的值和删除xml文件中节点的值。
首先我们认识一下xml文件有哪几种形式。下面我列出一些常用的xml文件的形式:
example1.xml:
<?xml version="1.0" ?>
<Hello>World</Hello>
example2.xml:
<?xml version="1.0" ?>
<poetry>
<verse>
Alas
Great World
Alas (again)
</verse>
</poetry>
example3.xml:
<?xml version="1.0" ?>
<shapes>
<circle name="int-based" x="20" y="30" r="50" />
<point name="float-based" x="3.5" y="52.1" />
</shapes>
example4.xml:
<?xml version="1.0" ?>
<MyApp>
<Messages>
<Welcome>Welcome to MyApp</Welcome>
<Farewell>Thank you for using MyApp</Farewell>
</Messages>
<Windows>
<Window name="MainFrame" x="5" y="15" w="400" h="250" />
</Windows>
<Connection ip="192.168.0.1" timeout="123.456000"/>
</MyApp>
Tinyxml使用了两种编译选择:使用标准C的char *类型或者使用STL中的std::string,其中使用预处理器TIXML_USE_STL进行控制,即添加了TIXML_USE_STL为使用std::string的。鉴于STL的广泛使用以及其强大功能,下面我以使用std::string的tinyxml说明。
首先使用VS 2005打开tinyxmlSTL.dsp的工程文件,将其编译成一个静态库,debug版本为:tinyxmld_STL.lib,然后开始测试tinyxml库。我的测试计划是这样的:首先使用tinyxml库创建example4.xml,然后将其读出来,然后查询指定节点的属性或文本,再修改example4.xml(修改其中的一些节点值和删除其中一个节点,增加一个节点),然后再读出来以判断是否修改成功。具体是在VS 2005上新建一个控制台工程:Test,注意使用多字节字符集进行编译,同时添加。
(1) 首先是创建xml文件的代码:
bool CreateXml(std::string XmlFile)
{
// 定义一个TiXmlDocument类指针
TiXmlDocument *pDoc =new TiXmlDocument;
if (NULL==pDoc)
{
return false;
}
//////////////////////声明//////////////////////////////////////////////////////////////////
TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(_T("1.0"),_T(""),_T(""));
if (NULL==pDeclaration)
{
return false;
}
pDoc->LinkEndChild(pDeclaration);
///////////////////////////////////////////////////////////////////////////////////////
/////////////////////节点//////////////////////////////////////////////////////////////////
// 生成一个根节点:MyApp
TiXmlElement *pRootEle = new TiXmlElement(_T("MyApp"));
if (NULL==pRootEle)
{
return false;
}
pDoc->LinkEndChild(pRootEle);
///////////////////////////////////////////////////////////////////////////////////////
////////////////////节点/////////////////////////////////////////////////////////////////
// 生成子节点:Messages
TiXmlElement *pMsg = new TiXmlElement(_T("Messages"));
if (NULL==pMsg)
{
return false;
}
pRootEle->LinkEndChild(pMsg);
////////////////////////////////////////////////////////////////////////////
// 生成子节点:Welcome
TiXmlElement *pWelcome = new TiXmlElement(_T("Welcome"));
if (NULL==pWelcome)
{
return false;
}
pMsg->LinkEndChild(pWelcome);
// 设置Welcome节点的值
std::string strValue = _T("Welcome to MyApp");
TiXmlText *pWelcomeValue = new TiXmlText(strValue);
pWelcome->LinkEndChild(pWelcomeValue);
// 生成子节点:Farewell
TiXmlElement *pFarewell = new TiXmlElement(_T("Farewell"));
if (NULL==pFarewell)
{
return false;
}
pMsg->LinkEndChild(pFarewell);
// 设置Farewell节点的值
strValue = _T("Thank you for using MyApp");
TiXmlText *pFarewellValue = new TiXmlText(strValue);
pFarewell->LinkEndChild(pFarewellValue);
// 生成子节点:Windows
TiXmlElement *pWindows = new TiXmlElement(_T("Windows"));
if (NULL==pWindows)
{
return false;
}
pRootEle->LinkEndChild(pWindows);
// 生成子节点:Window
TiXmlElement *pWindow = new TiXmlElement(_T("Window"));
if (NULL==pWindow)
{
return false;
}
pWindows->LinkEndChild(pWindow);
// 设置节点Window的值
pWindow->SetAttribute(_T("name"),_T("MainFrame"));
pWindow->SetAttribute(_T("x"),_T("5"));
pWindow->SetAttribute(_T("y"),_T("15"));
pWindow->SetAttribute(_T("w"),_T("400"));
pWindow->SetAttribute(_T("h"),_T("250"));
// 生成子节点:Connection
TiXmlElement *pConnection = new TiXmlElement(_T("Connection"));
if (NULL==pConnection)
{
return false;
}
pRootEle->LinkEndChild(pConnection);
// 设置节点Connection的值
pConnection->SetAttribute(_T("ip"),_T("192.168.0.1"));
pConnection->SetAttribute(_T("timeout"),_T("123.456000"));
////////////////////////////////////////////////////////////////////////////////////////
pDoc->SaveFile(XmlFile);
return true;
}
不知你注意到上面的规律没有?首先父节点连接字节点使用函数LinkEndChild,使用方法是:pParentNode-> LinkEndChild(pChild);其次设置类似这种结构<Window name="MainFrame" x="5" y="15" w="400" h="250" />采用SetAttribute函数,这个函数有两个参数,前一个参数表示键,后一个参数表示键值,设置<Farewell>Thank you for using MyApp</Farewell>这种结构采用TiXmlText类,使用LinkEndChild函数进行连结。
(2)下面介绍读取xml文件的代码
代码如下:
bool PaintXml(std::string XmlFile)
{
// 定义一个TiXmlDocument类指针
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)
{
return false;
}
pDoc->LoadFile(XmlFile);
pDoc->Print();
return true;
}
//////////////////////////////////////////////////////////////////////////////////////////////////
参考文献:
1.《TinyXML入门教程 》
2. 《tinyxml 使用笔记与总结 》
3. 《TinyXML Tutorial 中文指南 》
////////////////////////////////////////////////////////////////////////
(3)使用tinyxml库对xml文件进行一系列操作:获取xml文件声明,查询指定节点、删除指定节点、修改指定节点和增加节点
在《TinyXml快速入门(一)》中我们知道xml文件中的一个节点元素实际包含两种值:属性和文本。其中属性在我看来可以看作是STL中的map,一个属性带一个属性值,map中也是一个键带一个键值。
因此
查询指定节点、删除指定节点和增加节点必然是需要实现两种方法,
删除指定节点 只需要实现一种方法。
鉴于内容较多,在本文中介绍获取xml文件声明,查询指定节点、删除指定节点的做法,
修改指定节点和增加节点的做法在后续的文章介绍。
首先是获取xml文件声明。xml文件声明包括三方面的内容:Version、Standalone和Encoding。其源码如下:
bool GetXmlDeclare(std::string XmlFile,
std::string &strVersion,
std::string &strStandalone,
std::string &strEncoding)
{
// 定义一个TiXmlDocument类指针
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)
{
return false;
}
pDoc->LoadFile(XmlFile);
TiXmlNode* pXmlFirst = pDoc->FirstChild();
if (NULL != pXmlFirst)
{
TiXmlDeclaration* pXmlDec = pXmlFirst->ToDeclaration();
if (NULL != pXmlDec)
{
strVersion = pXmlDec->Version();
strStandalone = pXmlDec->Standalone();
strEncoding = pXmlDec->Encoding();
}
}
return true;
}
bool GetXmlDeclare(std::string XmlFile,
std::string &strVersion,
std::string &strStandalone,