TinyXml快速入门(三)

 在《TinyXml 快速入门(二) 》介绍使用tinyxml库获取xml文件声明,查询指定节点、删除指定节点的做法。在本文中继续介绍修改指定节点和增加节点的做法。


     修改节点其实和查询指定节点的值有点类似,也分为两个函数,一个实现修改文本。另一个负责修改属性。


[cpp]  view plain copy
  1. /*! 
  2. *  /brief 修改指定节点的文本。 
  3. * 
  4. *  /param XmlFile xml文件全路径。 
  5. *  /param strNodeName 指定的节点名。 
  6. *  /param strText 重新设定的文本的值 
  7. *  /return 是否成功。true为成功,false表示失败。 
  8. */  
  9. bool ModifyNode_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.         pNode->Clear();  // 首先清除所有文本  
  28.         // 然后插入文本,保存文件  
  29.         TiXmlText *pValue = new TiXmlText(strText);  
  30.         pNode->LinkEndChild(pValue);  
  31.         pDoc->SaveFile(XmlFile);  
  32.         return true;  
  33.     }  
  34.     else  
  35.         return false;  
  36. }  
  37. /*! 
  38. *  /brief 修改指定节点的属性值。 
  39. * 
  40. *  /param XmlFile xml文件全路径。 
  41. *  /param strNodeName 指定的节点名。 
  42. *  /param AttMap 重新设定的属性值,这是一个map,前一个为属性名,后一个为属性值 
  43. *  /return 是否成功。true为成功,false表示失败。 
  44. */  
  45. bool ModifyNode_Attribute(std::string XmlFile,std::string strNodeName,  
  46.                  std::map<std::string,std::string> &AttMap)  
  47. {  
  48.     typedef std::pair <std::string,std::string> String_Pair;  
  49.     // 定义一个TiXmlDocument类指针  
  50.     TiXmlDocument *pDoc = new TiXmlDocument();  
  51.     if (NULL==pDoc)  
  52.     {  
  53.         return false;  
  54.     }  
  55.     pDoc->LoadFile(XmlFile);  
  56.     TiXmlElement *pRootEle = pDoc->RootElement();  
  57.     if (NULL==pRootEle)  
  58.     {  
  59.         return false;  
  60.     }  
  61.    
  62.     TiXmlElement *pNode = NULL;  
  63.     GetNodePointerByName(pRootEle,strNodeName,pNode);  
  64.     if (NULL!=pNode)  
  65.     {  
  66.         TiXmlAttribute* pAttr = NULL;   
  67.         std::string strAttName = _T("");  
  68.         std::string strAttValue = _T("");  
  69.         for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())    
  70.         {    
  71.             strAttName = pAttr->Name();  
  72.             std::map<std::string,std::string>::iterator iter;  
  73.             for (iter=AttMap.begin();iter!=AttMap.end();iter++)  
  74.             {  
  75.                 if (strAttName==iter->first)  
  76.                 {  
  77.                     pAttr->SetValue(iter->second);  
  78.                 }  
  79.             }  
  80.         }    
  81.         pDoc->SaveFile(XmlFile);  
  82.         return true;  
  83.     }  
  84.     else  
  85.     {  
  86.         return false;  
  87.     }  
  88. }  


     对于ModifyNode_Attribute函数,这里稍微介绍一下如何使用,比如对于下面这样一个xml文件:

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" standalone="yes" ?>  
  2. <MyApp>  
  3.     <Messages>  
  4.         <Welcome>Welcome to MyApp</Welcome>  
  5.         <Farewell>Thank you for using MyApp</Farewell>  
  6.     </Messages>  
  7.     <Windows>  
  8.         <Window name="MainFrame" x="5" y="15" w="400" h="250" />  
  9.     </Windows>  
  10.     <Connection ip="192.168.0.1" timeout="123.456000" />  
  11. </MyApp>  


     我们如果要修改节点的Connection的ip为192.168.0.100,timeout为1000,我们可以这样用:


[cpp]  view plain copy
  1. std::string XmlFile = _T("E://TestTinyxml//example4.xml");  
  2.     std::string strNodeName = _T("Connection");  
  3.    typedef std::pair <std::string,std::string> String_Pair;  
  4.    std::map<std::string,std::string> AttMap;  
  5.    AttMap.insert(String_Pair(_T("ip"),_T("192.168.0.100")));  
  6.    AttMap.insert(String_Pair(_T("timeout"),_T("1000")));  
  7.    ModifyNode_Attribute(XmlFile,strNodeName,AttMap);  


     下面是增加节点的两个函数:


[cpp]  view plain copy
  1. /*! 
  2. *  /brief 增加指定节点的文本。 
  3. * 
  4. *  /param XmlFile xml文件全路径。 
  5. *  /param strParNodeName 要增加的节点的父节点。 
  6. *  /param strNodeName 指定的节点名。 
  7. *  /param strText 要增加的文本 
  8. *  /return 是否成功。true为成功,false表示失败。 
  9. */  
  10. bool AddNode_Text(std::string XmlFile,std::string strParNodeName,std::string strNodeName,std::string strText)  
  11. {  
  12.     // 定义一个TiXmlDocument类指针  
  13.     TiXmlDocument *pDoc = new TiXmlDocument();  
  14.     if (NULL==pDoc)  
  15.     {  
  16.         return false;  
  17.     }  
  18.     pDoc->LoadFile(XmlFile);  
  19.     TiXmlElement *pRootEle = pDoc->RootElement();  
  20.     if (NULL==pRootEle)  
  21.     {  
  22.         return false;  
  23.     }  
  24.     TiXmlElement *pNode = NULL;  
  25.     GetNodePointerByName(pRootEle,strParNodeName,pNode);  
  26.     if (NULL!=pNode)  
  27.     {  
  28.         // 生成子节点:pNewNode  
  29.         TiXmlElement *pNewNode = new TiXmlElement(strNodeName);  
  30.         if (NULL==pNewNode)  
  31.         {  
  32.             return false;  
  33.         }  
  34.         // 设置节点文本,然后插入节点  
  35.         TiXmlText *pNewValue = new TiXmlText(strText);  
  36.         pNewNode->LinkEndChild(pNewValue);  
  37.         pNode->InsertEndChild(*pNewNode);  
  38.         pDoc->SaveFile(XmlFile);  
  39.         return true;  
  40.     }  
  41.     else  
  42.          return false;  
  43.       
  44. }  
  45. /*! 
  46. *  /brief 增加节点。 
  47. * 
  48. *  /param XmlFile xml文件全路径。 
  49. *  /param strParNodeName 要增加的节点的父节点。 
  50. *  /param strNodeName 指定的节点名。 
  51. *  /param AttMap 要增加的节点设定的属性值,这是一个map,前一个为属性名,后一个为属性值 
  52. *  /return 是否成功。true为成功,false表示失败。 
  53. */  
  54. bool AddNode_Attribute(std::string XmlFile,std::string strParNodeName,std::string strNodeName,std::map<std::string,std::string> &AttMap)  
  55. {  
  56.     // 定义一个TiXmlDocument类指针  
  57.     TiXmlDocument *pDoc = new TiXmlDocument();  
  58.     if (NULL==pDoc)  
  59.     {  
  60.         return false;  
  61.     }  
  62.     pDoc->LoadFile(XmlFile);  
  63.     TiXmlElement *pRootEle = pDoc->RootElement();  
  64.     if (NULL==pRootEle)  
  65.     {  
  66.         return false;  
  67.     }  
  68.     TiXmlElement *pNode = NULL;  
  69.     GetNodePointerByName(pRootEle,strParNodeName,pNode);  
  70.     if (NULL!=pNode)  
  71.     {  
  72.         // 生成子节点:pNewNode  
  73.         TiXmlElement *pNewNode = new TiXmlElement(strNodeName);  
  74.         if (NULL==pNewNode)  
  75.         {  
  76.             return false;  
  77.         }  
  78.         // 设置节点的属性值,然后插入节点  
  79.         std::map<std::string,std::string>::iterator iter;  
  80.         for (iter=AttMap.begin();iter!=AttMap.end();iter++)  
  81.         {  
  82.              pNewNode->SetAttribute(iter->first,iter->second);  
  83.         }  
  84.         pNode->InsertEndChild(*pNewNode);  
  85.         pDoc->SaveFile(XmlFile);  
  86.         return true;  
  87.     }  
  88.     else  
  89.         return false;  
  90. }  


    

      至此tinyxml入门文章全部完成,相关源码下载请访问:
Tinyxml库及测试源码下载


参考文献:
1.《TinyXML入门教程 》

2. 《tinyxml 使用笔记与总结 》

3. 《TinyXML Tutorial 中文指南 》

4. 《使用TinyXml解析Xml示例 》


原文地址:http://blog.csdn.net/clever101/article/details/5379751


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值