TinyXml快速入门2

来源:互联网 发布:恐怖故事2剧情解析知乎 编辑:程序博客网 时间:2024/06/05 04:36
在《TinyXml快速入门(一)》中我介绍了使用TinyXml库如何创建和打印xml文件,下面我介绍使用tinyxml库对xml文件进行一系列的操作,包括获取xml文件声明,查询指定节点、删除指定节点、修改指定节点和增加节点的用法。在《TinyXml快速入门(一)》中我们知道xml文件中的一个节点元素实际包含两种值:属性和文本。其中属性在我看来可以看作是STL中的map,一个属性带一个属性值,map中也是一个键带一个键值。因此查询指定节点、删除指定节点和增加节点必然是需要实现两种方法,删除指定节点只需要实现一种方法。鉴于内容较多,在本文中介绍获取xml文件声明,查询指定节点、删除指定节点的做法,修改指定节点和增加节点的做法在后续的文章介绍。


          首先是获取xml文件声明。xml文件声明包括三方面的内容:Version、Standalone和Encoding。其源码如下:

 

01./*! 02.*  /brief 获取xml文件的声明。 03.* 04.*  /param XmlFile xml文件全路径。 05.*  /param strVersion  Version属性值 06.*  /param strStandalone Standalone属性值 07.*  /param strEncoding Encoding属性值 08.*  /return 是否成功。true为成功,false表示失败。 09.*/  10.bool GetXmlDeclare(std::string XmlFile,  11.                   std::string &strVersion,  12.                   std::string &strStandalone,  13.                   std::string &strEncoding)  14.{  15.    // 定义一个TiXmlDocument类指针  16.    TiXmlDocument *pDoc = new TiXmlDocument();  17.    if (NULL==pDoc)  18.    {  19.        return false;  20.    }  21.    pDoc->LoadFile(XmlFile);  22.      TiXmlNode* pXmlFirst = pDoc->FirstChild();     23.      if (NULL != pXmlFirst)    24.     {    25.          TiXmlDeclaration* pXmlDec = pXmlFirst->ToDeclaration();    26.          if (NULL != pXmlDec)    27.          {    28.              strVersion = pXmlDec->Version();  29.              strStandalone = pXmlDec->Standalone();  30.              strEncoding = pXmlDec->Encoding();  31.          }  32.      }  33.      return true;  34.}  


 


      我们发现无论查询节点、删除节点、修改节点和增加节点,其实都离不开一个函数,就是根据节点名获取相关节点指针。那么我们就先实现一个根据节点名获取节点指针的函数:

01./*! 02.*  /brief 通过根节点和节点名获取节点指针。 03.* 04.*  /param pRootEle   xml文件的根节点。 05.*  /param strNodeName  要查询的节点名 06.*  /param Node      需要查询的节点指针 07.*  /return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。 08.*/  09.bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node)  10.{  11.     // 假如等于根节点名,就退出  12.     if (strNodeName==pRootEle->Value())  13.     {  14.         Node = pRootEle;  15.         return true;  16.     }  17.      TiXmlElement* pEle = pRootEle;    18.      for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())    19.    {    20.          //递归处理子节点,获取节点指针  21.          if(GetNodePointerByName(pEle,strNodeName,Node))  22.              return true;  23.     }    24.     return false;  25.}   


 


01./*! 02.*  /brief 通过根节点和节点名获取节点指针。 03.* 04.*  /param pRootEle   xml文件的根节点。 05.*  /param strNodeName  要查询的节点名 06.*  /param Node      需要查询的节点指针 07.*  /return 是否找到。true为找到相应节点指针,false表示没有找到相应节点指针。 08.*/  09.bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node)  10.{  11.     // 假如等于根节点名,就退出  12.     if (strNodeName==pRootEle->Value())  13.     {  14.         Node = pRootEle;  15.         return true;  16.     }  17.      TiXmlElement* pEle = pRootEle;    18.      for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())    19.    {    20.          //递归处理子节点,获取节点指针  21.          if(GetNodePointerByName(pEle,strNodeName,Node))  22.              return true;  23.     }    24.     return false;  25.}   


有了这个函数,我们就很容易实现查询节点的相应文本或属性值。

 /*! 02.*  /brief 通过节点查询。 03.* 04.*  /param XmlFile   xml文件全路径。 05.*  /param strNodeName  要查询的节点名 06.*  /param strText      要查询的节点文本 07.*  /return 是否成功。true为成功,false表示失败。 08.*/  09.bool QueryNode_Text(std::string XmlFile,std::string strNodeName,std::string &strText)  10.{  11.    // 定义一个TiXmlDocument类指针  12.    TiXmlDocument *pDoc = new TiXmlDocument();  13.    if (NULL==pDoc)  14.    {  15.        return false;  16.    }  17.    pDoc->LoadFile(XmlFile);  18.    TiXmlElement *pRootEle = pDoc->RootElement();  19.    if (NULL==pRootEle)  20.    {  21.        return false;  22.    }  23.   TiXmlElement *pNode = NULL;  24.   GetNodePointerByName(pRootEle,strNodeName,pNode);  25.   if (NULL!=pNode)  26.   {  27.        strText = pNode->GetText();   28.        return true;  29.   }  30.   else  31.   {  32.        return false;  33.   }  34.      35.}  36./*! 37.*  /brief 通过节点查询。 38.* 39.*  /param XmlFile   xml文件全路径。 40.*  /param strNodeName  要查询的节点名 41.*  /param AttMap      要查询的属性值,这是一个map,前一个为属性名,后一个为属性值 42.*  /return 是否成功。true为成功,false表示失败。 43.*/  44.bool QueryNode_Attribute(std::string XmlFile,std::string strNodeName,std::map<std::string,std::string> &AttMap)  45.{  46.    // 定义一个TiXmlDocument类指针  47.    typedef std::pair <std::string,std::string> String_Pair;  48.    TiXmlDocument *pDoc = new TiXmlDocument();  49.    if (NULL==pDoc)  50.    {  51.        return false;  52.    }  53.    pDoc->LoadFile(XmlFile);  54.    TiXmlElement *pRootEle = pDoc->RootElement();  55.    if (NULL==pRootEle)  56.    {  57.        return false;  58.    }  59.    TiXmlElement *pNode = NULL;  60.    GetNodePointerByName(pRootEle,strNodeName,pNode);  61.    if (NULL!=pNode)  62.    {  63.        TiXmlAttribute* pAttr = NULL;   64.        for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())    65.        {    66.            std::string strAttName = pAttr->Name();  67.            std::string strAttValue = pAttr->Value();  68.            AttMap.insert(String_Pair(strAttName,strAttValue));  69.        }    70.        return true;  71.    }  72.    else  73.    {  74.        return false;  75.    }  76.    return true;  77.}  


下面是删除指定节点的函数,其中考虑了删除根节点的情况:下面是删除指定节点的函数,其中考虑了删除根节点的情况:下面是删除指定节点的函数,其中考虑了删除根节点的情况:

01./*! 02.*  /brief 删除指定节点的值。 03.* 04.*  /param XmlFile xml文件全路径。 05.*  /param strNodeName 指定的节点名。 06.*  /return 是否成功。true为成功,false表示失败。 07.*/  08.bool DelNode(std::string XmlFile,std::string strNodeName)  09.{  10.    // 定义一个TiXmlDocument类指针  11.    TiXmlDocument *pDoc = new TiXmlDocument();  12.    if (NULL==pDoc)  13.    {  14.        return false;  15.    }  16.    pDoc->LoadFile(XmlFile);  17.    TiXmlElement *pRootEle = pDoc->RootElement();  18.    if (NULL==pRootEle)  19.    {  20.        return false;  21.    }  22.    TiXmlElement *pNode = NULL;  23.    GetNodePointerByName(pRootEle,strNodeName,pNode);  24.    // 假如是根节点  25.    if (pRootEle==pNode)  26.    {  27.          if(pDoc->RemoveChild(pRootEle))  28.          {  29.               pDoc->SaveFile(XmlFile);  30.               return true;  31.          }  32.          else   33.              return false;  34.    }  35.    // 假如是其它节点  36.    if (NULL!=pNode)  37.    {  38.        TiXmlNode *pParNode =  pNode->Parent();  39.        if (NULL==pParNode)  40.        {  41.               return false;  42.        }  43.              44.        TiXmlElement* pParentEle = pParNode->ToElement();  45.        if (NULL!=pParentEle)  46.        {  47.            if(pParentEle->RemoveChild(pNode))  48.                 pDoc->SaveFile(XmlFile);  49.            else  50.                return false;  51.        }  52.    }  53.    else  54.    {  55.          return false;  56.    }  57.     return false;  58.}  


http://blog.csdn.net/clever101/article/details/5379466http://blog.csdn.net/clever101/article/details/5379466