c++通过tiny操作xml

tinyxml使用        

从网站上下载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操作

  1. // 定义一个TiXmlDocument类指针   
  2.    TiXmlDocument *pDoc = new TiXmlDocument;   
  3.    if (NULL==pDoc)   
  4.    {   
  5.        returnfalse;   
  6.    }   
  7.  
  8.    // 定义一个xml文件头部声明 
  9.    TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(("1.0"),(""),(""));   
  10.    if (NULL==pDeclaration)   
  11.    {   
  12.        returnfalse;   
  13.    } 
  14.   
  15.    pDoc->LinkEndChild(pDeclaration);   
  16.  
  17.    // 生成一个根节点:MyApp   
  18.    TiXmlElement *pRootEle = new TiXmlElement(("MyApp"));   
  19.    if (NULL==pRootEle)   
  20.    {   
  21.        returnfalse;   
  22.    }   
  23.    pDoc->LinkEndChild(pRootEle); 
  24.   
  25.    // 生成子节点:Messages   
  26.    TiXmlElement *pMsg = new TiXmlElement(("Messages"));   
  27.    if (NULL==pMsg)   
  28.    {   
  29.        returnfalse;   
  30.    }   
  31.    pRootEle->LinkEndChild(pMsg);   
  32.  
  33.    // 生成子节点:Welcome   
  34.    TiXmlElement *pWelcome = new TiXmlElement(("Welcome"));   
  35.    if (NULL==pWelcome)   
  36.    {   
  37.        returnfalse;   
  38.    }   
  39.    pMsg->LinkEndChild(pWelcome);   
  40.  
  41.    // 设置Welcome节点的值   
  42.    constchar* 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.        returnfalse;   
  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.        returnfalse;   
  64.    }   
  65.    pRootEle->LinkEndChild(pWindows);   
  66.  
  67.    // 生成子节点:Window   
  68.    TiXmlElement *pWindow = new TiXmlElement(("Window"));   
  69.    if (NULL==pWindow)   
  70.    {   
  71.        returnfalse;   
  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.        returnfalse;   
  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操作

  1. //TiXmlDocument *pDoc = new TiXmlDocument();   
  2.     if (NULL==pDoc)   
  3.     {   
  4.         returnfalse;   
  5.     }   
  6.     pDoc->LoadFile("1.xml");   
  7.     pDoc->Print();     

3.获取单个节点值

  1. bool GetNodePointerByName(TiXmlElement* pRootEle,std::string &strNodeName,TiXmlElement* &Node)   
  2. {   
  3.      // 假如等于根节点名,就退出   
  4.      if (strNodeName==pRootEle->Value())   
  5.      {   
  6.          Node = pRootEle;   
  7.          returntrue;   
  8.      }   
  9.       TiXmlElement* pEle = pRootEle;     
  10.       for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())     
  11.     {     
  12.           //递归处理子节点,获取节点指针   
  13.           if(GetNodePointerByName(pEle,strNodeName,Node))   
  14.               returntrue;   
  15.      }     
  16.      returnfalse;   
  17. }    
 
  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.         returnfalse;   
  8.     }   
  9.     pDoc->LoadFile(XmlFile);   
  10.     TiXmlElement *pRootEle = pDoc->RootElement();   
  11.     if (NULL==pRootEle)   
  12.     {   
  13.         returnfalse;   
  14.     }   
  15.    TiXmlElement *pNode = NULL;   
  16.    GetNodePointerByName(pRootEle,strNodeName,pNode);   
  17.    if (NULL!=pNode)   
  18.    {   
  19.         strText = pNode->GetText();    
  20.         returntrue;   
  21.    }   
  22.    else   
  23.    {   
  24.         returnfalse;   
  25.    }   
  26.        
  27. }   
 
  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.         returnfalse;   
  9.     }   
  10.     pDoc->LoadFile(XmlFile);   
  11.     TiXmlElement *pRootEle = pDoc->RootElement();   
  12.     if (NULL==pRootEle)   
  13.     {   
  14.         returnfalse;   
  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.         returntrue;   
  28.     }   
  29.     else   
  30.     {   
  31.         returnfalse;   
  32.     }   
  33.     returntrue;   
  34. }   
 

4.删除节点操作

 

  1. bool DelNode(std::string XmlFile,std::string strNodeName)   
  2. {   
  3.     // 定义一个TiXmlDocument类指针   
  4.     TiXmlDocument *pDoc = new TiXmlDocument();   
  5.     if (NULL==pDoc)   
  6.     {   
  7.         returnfalse;   
  8.     }   
  9.     pDoc->LoadFile(XmlFile);   
  10.     TiXmlElement *pRootEle = pDoc->RootElement();   
  11.     if (NULL==pRootEle)   
  12.     {   
  13.         returnfalse;   
  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.                returntrue;   
  24.           }   
  25.           else    
  26.               returnfalse;   
  27.     }   
  28.     // 假如是其它节点   
  29.     if (NULL!=pNode)   
  30.     {   
  31.         TiXmlNode *pParNode =  pNode->Parent();   
  32.         if (NULL==pParNode)   
  33.         {   
  34.                returnfalse;   
  35.         }   
  36.                
  37.         TiXmlElement* pParentEle = pParNode->ToElement();   
  38.         if (NULL!=pParentEle)   
  39.         {   
  40.             if(pParentEle->RemoveChild(pNode))   
  41.                  pDoc->SaveFile(XmlFile);   
  42.             else   
  43.                 returnfalse;   
  44.         }   
  45.     }   
  46.     else   
  47.     {   
  48.           returnfalse;   
  49.     }   
  50.      returnfalse;   
  51. }   
 

5.修改节点操作

 

  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.         returnfalse;   
  8.     }   
  9.     pDoc->LoadFile(XmlFile);   
  10.     TiXmlElement *pRootEle = pDoc->RootElement();   
  11.     if (NULL==pRootEle)   
  12.     {   
  13.         returnfalse;   
  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.         returntrue;   
  25.     }   
  26.     else   
  27.         returnfalse;   
  28. }   
 
  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.         returnfalse;   
  10.     }   
  11.     pDoc->LoadFile(XmlFile);   
  12.     TiXmlElement *pRootEle = pDoc->RootElement();   
  13.     if (NULL==pRootEle)   
  14.     {   
  15.         returnfalse;   
  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.         returntrue;   
  39.     }   
  40.     else   
  41.     {   
  42.         returnfalse;   
  43.     }   
  44. }   
 

6增加节点操作

 

  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.         returnfalse;   
  8.     }   
  9.     pDoc->LoadFile(XmlFile);   
  10.     TiXmlElement *pRootEle = pDoc->RootElement();   
  11.     if (NULL==pRootEle)   
  12.     {   
  13.         returnfalse;   
  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.             returnfalse;   
  24.         }   
  25.         // 设置节点文本,然后插入节点   
  26.         TiXmlText *pNewValue = new TiXmlText(strText);   
  27.         pNewNode->LinkEndChild(pNewValue);   
  28.         pNode->InsertEndChild(*pNewNode);   
  29.         pDoc->SaveFile(XmlFile);   
  30.         returntrue;   
  31.     }   
  32.     else   
  33.          returnfalse;   
  34.        
  35. }   
 
  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.         returnfalse;   
  8.     }   
  9.     pDoc->LoadFile(XmlFile);   
  10.     TiXmlElement *pRootEle = pDoc->RootElement();   
  11.     if (NULL==pRootEle)   
  12.     {   
  13.         returnfalse;   
  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.             returnfalse;   
  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.         returntrue;   
  34.     }   
  35.     else   
  36.         returnfalse;   
  37. }   

posted on 2012-11-30 16:48  静静的白色外套  阅读(391)  评论(0)    收藏  举报

导航