tinyXml是一个非常不错的轻量级的xml文件操作库,一共就6个代码文件,其中头文件2个,源文件4个。可以非常方便的集成到自己的代码中。我已经整理好了tinyXml操作库,简单包装了下tinyxml,当然参考了网上一些博客的代码,并加以整理和修整。
包装了5个函数,实现了如下功能:
1.获取xml声明信息
bool GetXmlDeclare(const char* file_name,string &version,string &standalone,string &encode)
{
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)return false;
pDoc->LoadFile(file_name);
TiXmlNode* pXmlFirst = pDoc->FirstChild();
if (NULL != pXmlFirst)
{
TiXmlDeclaration* pXmlDec = pXmlFirst->ToDeclaration();
if (NULL != pXmlDec)
{
version = pXmlDec->Version();
standalone = pXmlDec->Standalone();
encode = pXmlDec->Encoding();
}
}
return true;
}
2.根据节点名获取节点指针,这是后面函数的基础
bool GetNodePointerByName(TiXmlElement* root,string &node_name,TiXmlElement* &node)
{
//等于根节点名,就退出
if (node_name==root->Value())
{
node = root;
return true;
}
TiXmlElement* pEle = root;
for (pEle = root->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
//递归处理子节点,获取节点指针
if(GetNodePointerByName(pEle,node_name,node))return true;
}
return false;
}
3.指定节点名称,获取节点的文本内容
bool GetNodeText(const char* file_name,string node_name,string &node_text)
{
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)return false;
pDoc->LoadFile(file_name);
TiXmlElement *pRootEle = pDoc->RootElement();
if (NULL==pRootEle)return false;
TiXmlElement *pNode = NULL;
GetNodePointerByName(pRootEle,node_name,pNode);
if (NULL!=pNode)
{
node_text = pNode->GetText();
return true;
}
return false;
}
4.指定节点名称,设置节点的文本内容
bool SetNodeText(const char* file_name,string node_name,const char* strText)
{
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)return false;
pDoc->LoadFile(file_name);
TiXmlElement *pRootEle = pDoc->RootElement();
if (NULL==pRootEle)return false;
TiXmlElement *pNode = NULL;
GetNodePointerByName(pRootEle,node_name,pNode);
if (NULL!=pNode)
{
pNode->Clear();
TiXmlText *pValue = new TiXmlText(strText);
pNode->LinkEndChild(pValue);
pDoc->SaveFile(file_name);
return true;
}
else
return false;
}
5.删除节点
bool DeleteNode(const char* file_name,string node_name)
{
TiXmlDocument *pDoc = new TiXmlDocument();
if (NULL==pDoc)return false;
pDoc->LoadFile(file_name);
TiXmlElement *pRootEle = pDoc->RootElement();
if (NULL==pRootEle)return false;
TiXmlElement *pNode = NULL;
GetNodePointerByName(pRootEle,node_name,pNode);
// 根节点
if (pRootEle==pNode)
{
if(pDoc->RemoveChild(pRootEle))
{
pDoc->SaveFile(file_name);
return true;
}
else
return false;
}
// 其它节点
if (NULL!=pNode)
{
TiXmlNode *pParNode = pNode->Parent();
if (NULL==pParNode)return false;
TiXmlElement* pParentEle = pParNode->ToElement();
if (NULL!=pParentEle)
{
if(pParentEle->RemoveChild(pNode))pDoc->SaveFile(file_name);
else return false;
}
}
else return false;
return false;
}
其中需要包含的头文件信息如下:
#include "tinyxml.h"
#include <string>
#include <iostream>
using namespace std;
tinyxml源码文件下载:点此去下载tinyxml源码
测试代码如下:
void main()
{
string ver,stand,encode;
GetXmlDeclare("C:/setting.xml",ver,stand,encode);
string val;
GetNodeText("C:/setting.xml","ip",val);
cout<<val<<endl;
SetNodeText("C:/setting.xml","ip","www.00.com");
GetNodeText("C:/setting.xml","ip",val);
cout<<val<<endl;
DeleteNode("C:/setting.xml","Welcome2");
}
注意事项:tinyxml基于完整的路径名来操作xml文件,所以要传递完整的路径名,不要用相对路径名哦。