在《TinyXml 快速入门(二)》介绍使用tinyxml库获取xml文件声明,查询指定节点、删除指定节点的做法。在本文中继续介绍修改指定节点和增加节点的做法。
修改节点其实和查询指定节点的值有点类似,也分为两个函数,一个实现修改文本。另一个负责修改属性。
01./*!
02.* /brief 修改指定节点的文本。
03.*
04.* /param XmlFile xml文件全路径。
05.* /param strNodeName 指定的节点名。
06.* /param strText 重新设定的文本的值
07.* /return 是否成功。true为成功,false表示失败。
08.*/
09.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文件:
01.<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
02.<MyApp>
03. <Messages>
04. <Welcome>Welcome to MyApp</Welcome>
05. <Farewell>Thank you for using MyApp</Farewell>
06. </Messages>
07. <Windows>
08. <Window name="MainFrame" x="5" y="15" w="400" h="250" />
09. </Windows>
10. <Connection ip="192.168.0.1" timeout="123.456000" />
11.</MyApp>
我们如果要修改节点的Connection的ip为192.168.0.100,timeout为1000,我们可以这样用:
01.std::string XmlFile = _T("E://TestTinyxml//example4.xml");
02. std::string strNodeName = _T("Connection");
03. typedef std::pair <std::string,std::string> String_Pair;
04. std::map<std::string,std::string> AttMap;
05. AttMap.insert(String_Pair(_T("ip"),_T("192.168.0.100")));
06. AttMap.insert(String_Pair(_T("timeout"),_T("1000")));
07. ModifyNode_Attribute(XmlFile,strNodeName,AttMap);
下面是增加节点的两个函数:
01./*!
02.* /brief 增加指定节点的文本。
03.*
04.* /param XmlFile xml文件全路径。
05.* /param strParNodeName 要增加的节点的父节点。
06.* /param strNodeName 指定的节点名。
07.* /param strText 要增加的文本
08.* /return 是否成功。true为成功,false表示失败。
09.*/
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.}