基于开源tinyxml的配置文件类

大部分算是原创吧,自己用了下还是挺好用的,比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();  
 
}  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值