Tinyxml的使用

来源:互联网 发布:mac访问windows文件夹 编辑:程序博客网 时间:2024/05/21 18:00

现在越来越多的数据和配置采用了xml格式来存放和进行传输解析了。在c++方面,没有本地支持的库,所以需要我们自己去找一下。微软的msxml说实话,确实不咋地,尤其是com的类型变量名字一直指针,让众人看上去就比较反感。开源的tinyxml在这方便做的还不错。简单介绍下使用过程的一点小经验。

在这里发下牢骚,VC6.0以后的各个版本的VS环境对于C++的智能感知都是那么的SB,不管你怎么配置,怎么google都让你非常抓狂,就是不出来。算了不说了。


从网站上下载tinyxml,下载之后解压打开文件夹,里面有一些测试例子,tinyxml.sln支持vs2010了都,不管这些,怎么需要的是那个xml类库。

使用tinyxml我们只需要

tinyxml.cpp, 

tinyxml.h, 

tinyxmlerror.cpp, 

tinyxmlparser.cpp, 

tinystr.cpp, 

tinystr.h

6个文件即可。注意一旦少拷贝了其中tinyxmlerror.cpp, tinyxmlparser.cpp, 其中一个或者两个,就会报告link错误,呵呵

在目标源文件的头部,添加 #include"tinyxml.h"和#include "tinystr.h"


我们使用xml文件,无外乎这几种操作,

1. 遍历整个xml返回一棵树select

2. 查找特定节点的属性/文本值select

3.插入特定位置一个节点insert

4.更新特定节点的属性/文本值 update

5.创建xml文件 create

6.who knows


据一位网友的博文里面提到,基本应该涵盖数据库的所有操作,xml操作应该像操作数据库一样。我觉得甚是有道理啊。

那么我来列举下,我搜集以及测试成功的相关的操作代码吧,希望能节省一些大家学习的时间。


1.create xml操作

[cpp] view plaincopy
  1. // 定义一个TiXmlDocument类指针    
  2.    TiXmlDocument *pDoc = new TiXmlDocument;    
  3.    if (NULL==pDoc)    
  4.    {    
  5.        return false;    
  6.    }    
  7.   
  8.    // 定义一个xml文件头部声明  
  9.    TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(("1.0"),(""),(""));    
  10.    if (NULL==pDeclaration)    
  11.    {    
  12.        return false;    
  13.    }  
  14.    
  15.    pDoc->LinkEndChild(pDeclaration);    
  16.   
  17.    // 生成一个根节点:MyApp    
  18.    TiXmlElement *pRootEle = new TiXmlElement(("MyApp"));    
  19.    if (NULL==pRootEle)    
  20.    {    
  21.        return false;    
  22.    }    
  23.    pDoc->LinkEndChild(pRootEle);  
  24.    
  25.    // 生成子节点:Messages    
  26.    TiXmlElement *pMsg = new TiXmlElement(("Messages"));    
  27.    if (NULL==pMsg)    
  28.    {    
  29.        return false;    
  30.    }    
  31.    pRootEle->LinkEndChild(pMsg);    
  32.   
  33.    // 生成子节点:Welcome    
  34.    TiXmlElement *pWelcome = new TiXmlElement(("Welcome"));    
  35.    if (NULL==pWelcome)    
  36.    {    
  37.        return false;    
  38.    }    
  39.    pMsg->LinkEndChild(pWelcome);    
  40.   
  41.    // 设置Welcome节点的值    
  42.    const char* strValue = ("Welcome to MyApp");    
  43.    TiXmlText *pWelcomeValue = new TiXmlText(strValue);    
  44.    pWelcome->LinkEndChild(pWelcomeValue);    
  45.   
  46.    // 生成子节点:Farewell    
  47.    TiXmlElement *pFarewell = new TiXmlElement(("Farewell"));    
  48.    if (NULL==pFarewell)    
  49.    {    
  50.        return false;    
  51.    }    
  52.    pMsg->LinkEndChild(pFarewell);   
  53.   
  54.    // 设置Farewell节点的值    
  55.    strValue = ("Thank you for using MyApp");    
  56.    TiXmlText *pFarewellValue = new TiXmlText(strValue);    
  57.    pFarewell->LinkEndChild(pFarewellValue);    
  58.   
  59.    // 生成子节点:Windows    
  60.    TiXmlElement *pWindows = new TiXmlElement(("Windows"));    
  61.    if (NULL==pWindows)    
  62.    {    
  63.        return false;    
  64.    }    
  65.    pRootEle->LinkEndChild(pWindows);    
  66.   
  67.    // 生成子节点:Window    
  68.    TiXmlElement *pWindow = new TiXmlElement(("Window"));    
  69.    if (NULL==pWindow)    
  70.    {    
  71.        return false;    
  72.    }    
  73.    pWindows->LinkEndChild(pWindow);   
  74.   
  75.    // 设置节点Window的值    
  76.    pWindow->SetAttribute(("name"),("MainFrame"));    
  77.    pWindow->SetAttribute(("x"),("5"));    
  78.    pWindow->SetAttribute(("y"),("15"));    
  79.    pWindow->SetAttribute(("w"),("400"));    
  80.    pWindow->SetAttribute(("h"),("250"));  
  81.    
  82.    // 生成子节点:Window    
  83.    TiXmlElement *pConnection  = new TiXmlElement(("Connection"));    
  84.    if (NULL==pConnection)    
  85.    {    
  86.        return false;    
  87.    }    
  88.    pRootEle->LinkEndChild(pConnection);    
  89.   
  90.    // 设置节点Connection的值    
  91.    pConnection->SetAttribute(("ip"),("192.168.0.1"));    
  92.    pConnection->SetAttribute(("timeout"),("123.456000"));    
  93.    pDoc->SaveFile("1.xml");    

2.遍历打印xml文件 select操作

[cpp] view plaincopy
  1. //TiXmlDocument *pDoc = new TiXmlDocument();    
  2.     if (NULL==pDoc)    
  3.     {    
  4.         return false;    
  5.     }    
  6.     pDoc->LoadFile("1.xml");    
  7.     pDoc->Print();    

3.获取单个节点值

[cpp] view plaincopy
  1. bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node)    
  2. {    
  3.      // 假如等于根节点名,就退出    
  4.      if (strNodeName==pRootEle->Value())    
  5.      {    
  6.          Node = pRootEle;    
  7.          return true;    
  8.      }    
  9.       TiXmlElement* pEle = pRootEle;      
  10.       for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())      
  11.     {      
  12.           //递归处理子节点,获取节点指针    
  13.           if(GetNodePointerByName(pEle,strNodeName,Node))    
  14.               return true;    
  15.      }      
  16.      return false;    
  17. }     

[cpp] view plaincopy
  1. bool QueryNode_Text(std::string XmlFile,std::string strNodeName,std::string &strText)    
  2. {    
  3.     // 定义一个TiXmlDocument类指针    
  4.     TiXmlDocument *pDoc = new TiXmlDocument();    
  5.     if (NULL==pDoc)    
  6.     {    
  7.         return false;    
  8.     }    
  9.     pDoc->LoadFile(XmlFile);    
  10.     TiXmlElement *pRootEle = pDoc->RootElement();    
  11.     if (NULL==pRootEle)    
  12.     {    
  13.         return false;    
  14.     }    
  15.    TiXmlElement *pNode = NULL;    
  16.    GetNodePointerByName(pRootEle,strNodeName,pNode);    
  17.    if (NULL!=pNode)    
  18.    {    
  19.         strText = pNode->GetText();     
  20.         return true;    
  21.    }    
  22.    else    
  23.    {    
  24.         return false;    
  25.    }    
  26.         
  27. }    

[cpp] view plaincopy
  1. bool QueryNode_Attribute(std::string XmlFile,std::string strNodeName,std::map<std::string,std::string> &AttMap)    
  2. {    
  3.     // 定义一个TiXmlDocument类指针    
  4.     typedef std::pair <std::string,std::string> String_Pair;    
  5.     TiXmlDocument *pDoc = new TiXmlDocument();    
  6.     if (NULL==pDoc)    
  7.     {    
  8.         return false;    
  9.     }    
  10.     pDoc->LoadFile(XmlFile);    
  11.     TiXmlElement *pRootEle = pDoc->RootElement();    
  12.     if (NULL==pRootEle)    
  13.     {    
  14.         return false;    
  15.     }    
  16.     TiXmlElement *pNode = NULL;    
  17.     GetNodePointerByName(pRootEle,strNodeName,pNode);    
  18.     if (NULL!=pNode)    
  19.     {    
  20.         TiXmlAttribute* pAttr = NULL;     
  21.         for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())      
  22.         {      
  23.             std::string strAttName = pAttr->Name();    
  24.             std::string strAttValue = pAttr->Value();    
  25.             AttMap.insert(String_Pair(strAttName,strAttValue));    
  26.         }      
  27.         return true;    
  28.     }    
  29.     else    
  30.     {    
  31.         return false;    
  32.     }    
  33.     return true;    
  34. }    

4.删除节点操作

[cpp] view plaincopy
  1. bool DelNode(std::string XmlFile,std::string strNodeName)    
  2. {    
  3.     // 定义一个TiXmlDocument类指针    
  4.     TiXmlDocument *pDoc = new TiXmlDocument();    
  5.     if (NULL==pDoc)    
  6.     {    
  7.         return false;    
  8.     }    
  9.     pDoc->LoadFile(XmlFile);    
  10.     TiXmlElement *pRootEle = pDoc->RootElement();    
  11.     if (NULL==pRootEle)    
  12.     {    
  13.         return false;    
  14.     }    
  15.     TiXmlElement *pNode = NULL;    
  16.     GetNodePointerByName(pRootEle,strNodeName,pNode);    
  17.     // 假如是根节点    
  18.     if (pRootEle==pNode)    
  19.     {    
  20.           if(pDoc->RemoveChild(pRootEle))    
  21.           {    
  22.                pDoc->SaveFile(XmlFile);    
  23.                return true;    
  24.           }    
  25.           else     
  26.               return false;    
  27.     }    
  28.     // 假如是其它节点    
  29.     if (NULL!=pNode)    
  30.     {    
  31.         TiXmlNode *pParNode =  pNode->Parent();    
  32.         if (NULL==pParNode)    
  33.         {    
  34.                return false;    
  35.         }    
  36.                 
  37.         TiXmlElement* pParentEle = pParNode->ToElement();    
  38.         if (NULL!=pParentEle)    
  39.         {    
  40.             if(pParentEle->RemoveChild(pNode))    
  41.                  pDoc->SaveFile(XmlFile);    
  42.             else    
  43.                 return false;    
  44.         }    
  45.     }    
  46.     else    
  47.     {    
  48.           return false;    
  49.     }    
  50.      return false;    
  51. }    

5.修改节点操作

[cpp] view plaincopy
  1. bool ModifyNode_Text(std::string XmlFile,std::string strNodeName,std::string strText)    
  2. {    
  3.     // 定义一个TiXmlDocument类指针    
  4.     TiXmlDocument *pDoc = new TiXmlDocument();    
  5.     if (NULL==pDoc)    
  6.     {    
  7.         return false;    
  8.     }    
  9.     pDoc->LoadFile(XmlFile);    
  10.     TiXmlElement *pRootEle = pDoc->RootElement();    
  11.     if (NULL==pRootEle)    
  12.     {    
  13.         return false;    
  14.     }    
  15.     TiXmlElement *pNode = NULL;    
  16.     GetNodePointerByName(pRootEle,strNodeName,pNode);    
  17.     if (NULL!=pNode)    
  18.     {    
  19.         pNode->Clear();  // 首先清除所有文本    
  20.         // 然后插入文本,保存文件    
  21.         TiXmlText *pValue = new TiXmlText(strText);    
  22.         pNode->LinkEndChild(pValue);    
  23.         pDoc->SaveFile(XmlFile);    
  24.         return true;    
  25.     }    
  26.     else    
  27.         return false;    
  28. }    

[cpp] view plaincopy
  1. bool ModifyNode_Attribute(std::string XmlFile,std::string strNodeName,    
  2.                  std::map<std::string,std::string> &AttMap)    
  3. {    
  4.     typedef std::pair <std::string,std::string> String_Pair;    
  5.     // 定义一个TiXmlDocument类指针    
  6.     TiXmlDocument *pDoc = new TiXmlDocument();    
  7.     if (NULL==pDoc)    
  8.     {    
  9.         return false;    
  10.     }    
  11.     pDoc->LoadFile(XmlFile);    
  12.     TiXmlElement *pRootEle = pDoc->RootElement();    
  13.     if (NULL==pRootEle)    
  14.     {    
  15.         return false;    
  16.     }    
  17.      
  18.     TiXmlElement *pNode = NULL;    
  19.     GetNodePointerByName(pRootEle,strNodeName,pNode);    
  20.     if (NULL!=pNode)    
  21.     {    
  22.         TiXmlAttribute* pAttr = NULL;     
  23.         std::string strAttName = _T("");    
  24.         std::string strAttValue = _T("");    
  25.         for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())      
  26.         {      
  27.             strAttName = pAttr->Name();    
  28.             std::map<std::string,std::string>::iterator iter;    
  29.             for (iter=AttMap.begin();iter!=AttMap.end();iter++)    
  30.             {    
  31.                 if (strAttName==iter->first)    
  32.                 {    
  33.                     pAttr->SetValue(iter->second);    
  34.                 }    
  35.             }    
  36.         }      
  37.         pDoc->SaveFile(XmlFile);    
  38.         return true;    
  39.     }    
  40.     else    
  41.     {    
  42.         return false;    
  43.     }    
  44. }    

6增加节点操作

[cpp] view plaincopy
  1. bool AddNode_Text(std::string XmlFile,std::string strParNodeName,std::string strNodeName,std::string strText)    
  2. {    
  3.     // 定义一个TiXmlDocument类指针    
  4.     TiXmlDocument *pDoc = new TiXmlDocument();    
  5.     if (NULL==pDoc)    
  6.     {    
  7.         return false;    
  8.     }    
  9.     pDoc->LoadFile(XmlFile);    
  10.     TiXmlElement *pRootEle = pDoc->RootElement();    
  11.     if (NULL==pRootEle)    
  12.     {    
  13.         return false;    
  14.     }    
  15.     TiXmlElement *pNode = NULL;    
  16.     GetNodePointerByName(pRootEle,strParNodeName,pNode);    
  17.     if (NULL!=pNode)    
  18.     {    
  19.         // 生成子节点:pNewNode    
  20.         TiXmlElement *pNewNode = new TiXmlElement(strNodeName);    
  21.         if (NULL==pNewNode)    
  22.         {    
  23.             return false;    
  24.         }    
  25.         // 设置节点文本,然后插入节点    
  26.         TiXmlText *pNewValue = new TiXmlText(strText);    
  27.         pNewNode->LinkEndChild(pNewValue);    
  28.         pNode->InsertEndChild(*pNewNode);    
  29.         pDoc->SaveFile(XmlFile);    
  30.         return true;    
  31.     }    
  32.     else    
  33.          return false;    
  34.         
  35. }    

[cpp] view plaincopy
  1. bool AddNode_Attribute(std::string XmlFile,std::string strParNodeName,std::string strNodeName,std::map<std::string,std::string> &AttMap)    
  2. {    
  3.     // 定义一个TiXmlDocument类指针    
  4.     TiXmlDocument *pDoc = new TiXmlDocument();    
  5.     if (NULL==pDoc)    
  6.     {    
  7.         return false;    
  8.     }    
  9.     pDoc->LoadFile(XmlFile);    
  10.     TiXmlElement *pRootEle = pDoc->RootElement();    
  11.     if (NULL==pRootEle)    
  12.     {    
  13.         return false;    
  14.     }    
  15.     TiXmlElement *pNode = NULL;    
  16.     GetNodePointerByName(pRootEle,strParNodeName,pNode);    
  17.     if (NULL!=pNode)    
  18.     {    
  19.         // 生成子节点:pNewNode    
  20.         TiXmlElement *pNewNode = new TiXmlElement(strNodeName);    
  21.         if (NULL==pNewNode)    
  22.         {    
  23.             return false;    
  24.         }    
  25.         // 设置节点的属性值,然后插入节点    
  26.         std::map<std::string,std::string>::iterator iter;    
  27.         for (iter=AttMap.begin();iter!=AttMap.end();iter++)    
  28.         {    
  29.              pNewNode->SetAttribute(iter->first,iter->second);    
  30.         }    
  31.         pNode->InsertEndChild(*pNewNode);    
  32.         pDoc->SaveFile(XmlFile);    
  33.         return true;    
  34.     }    
  35.     else    
  36.         return false;    
  37. }    

如果你想了解更多的操作以及细化,请参看参考文档了。

0 0
原创粉丝点击