大部分算是原创吧,自己用了下还是挺好用的,比bcb的TiniFile好用吧
/*——————————————————————————————————————————————
** FileName: config.h
** Author: wuding
** Date: Apr 30, 2008
** Comment: 配置文件类,主要用来读取xml配置文件中的一些配置信息
*/
头文件:
#ifndef CONFIGH
#define CONFIGH
#include "EString.h"
#include "tinyxml.h"
#include "tinystr.h"
#pragma comment (lib,"tinyxml.lib")
class EConfig
{
public:
explicit EConfig(const char* xmlFileName);
public:
//设置节点的值,如果没有就新增
bool SetValue(const EString &,const EString &);
//得到nodeName结点的值
EString GetValue(const EString &);
bool SetAttr(const EString &, const EString &, const EString &);
EString GetAttr(const EString &, const EString &);
bool SaveFile();
private:
//禁止默认构造函数被调用
EConfig();
private:
TiXmlDocument mXmlConfigFile;
TiXmlElement *mRootElem;
TiXmlElement *mCurrElem;
TiXmlNode *mCurrpNode;
};
#endif
实现文件:
/*
** FileName: config.cpp
** Author: hansen
** Date: Apr 30, 2007
** Comment:
*/
#include "config.h"
EConfig::EConfig(const char* xmlFileName)
:mXmlConfigFile(xmlFileName),mRootElem(0)
{
mCurrElem =NULL;
mCurrpNode =NULL;
//加载配置文件
if(!mXmlConfigFile.LoadFile())
{
TiXmlDeclaration Declaration( "1.0","gb2312", "yes" ); // 声明XML的属性
mXmlConfigFile.InsertEndChild( Declaration ); // 写入基本的XML头结构
TiXmlElement pRootElm( "Config" );
mXmlConfigFile.InsertEndChild(pRootElm) ;
mXmlConfigFile.SaveFile(); // 把XML文件写入硬盘
}
mXmlConfigFile.LoadFile();
//得到配置文件的根结点
mRootElem=mXmlConfigFile.RootElement();
}
bool EConfig::SaveFile()
{
return mXmlConfigFile.SaveFile();
}
//----------------------------------
EString EConfig::GetAttr(const EString &nodeName, const EString &attrName)
{
if(!mRootElem)
{
printf("读取根结点出错/n");
return "";
}
TiXmlElement *pElem=NULL;
pElem =mRootElem->FirstChildElement(nodeName.c_str());
if(NULL ==pElem) //如果该节点不存在
return "";
return pElem->Attribute(attrName.c_str()) ;
}
bool EConfig::SetAttr(const EString &nodeName, const EString &attrName, const EString &attrValue)
{
if(!mRootElem)
{
printf("读取根结点出错/n");
return false;
}
TiXmlElement *pElem=NULL;
pElem =mRootElem->FirstChildElement(nodeName.c_str());
if(NULL ==pElem) //如果该节点不存在
return false;
pElem->SetAttribute(attrName.c_str(),attrValue.c_str()) ;
}
//----------------------------------------------------------------
bool EConfig::SetValue(const EString &nodeName,const EString &nodeValue)
{
if(!mRootElem)
{
printf("读取根结点出错/n");
return false;
}
TiXmlElement *pElem=NULL;
TiXmlNode *pNode=NULL;
TiXmlText pText(nodeValue.c_str()) ;
pElem =mRootElem->FirstChildElement(nodeName.c_str());
if(NULL !=pElem) //如果该节点已存在,就删除它
mRootElem->RemoveChild (pElem);
//在Root节点下面插入一个节点
TiXmlElement Childe(nodeName.c_str()) ;
pNode = mRootElem->InsertEndChild(Childe) ;
if(NULL!=pNode)
pElem = pNode->ToElement() ;
if(NULL!=pElem)
pElem->InsertEndChild(pText) ;
if(pElem && pNode)
{
mCurrElem =pElem;
mCurrpNode =pNode;
return true;
}
return false;
}
EString EConfig::GetValue(const EString &nodeName)
{
if(!mRootElem)
{
printf("读取根结点出错/n");
return "";
}
TiXmlElement *pElem=mRootElem->FirstChildElement(nodeName.c_str());
if(!pElem)
{
printf("读取%s结点出错/n",nodeName.c_str());
return "";
}
return pElem->GetText();
}