作者:朱金灿
来源:http://blog.csdn.net/clever101
在《TinyXml 快速入门(二) 》介绍使用tinyxml库获取xml文件声明,查询指定节点、删除指定节点的做法。在本文中继续介绍修改指定节点和增加节点的做法。
修改节点其实和查询指定节点的值有点类似,也分为两个函数,一个实现修改文本。另一个负责修改属性。
-
-
-
-
-
-
-
-
- bool ModifyNode_Text(std::string XmlFile,std::string strNodeName,std::string strText)
- {
-
- TiXmlDocument *pDoc = new TiXmlDocument();
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile);
- TiXmlElement *pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement *pNode = NULL;
- GetNodePointerByName(pRootEle,strNodeName,pNode);
- if (NULL!=pNode)
- {
- pNode->Clear();
-
- TiXmlText *pValue = new TiXmlText(strText);
- pNode->LinkEndChild(pValue);
- pDoc->SaveFile(XmlFile);
- return true;
- }
- else
- return false;
- }
-
-
-
-
-
-
-
-
- bool ModifyNode_Attribute(std::string XmlFile,std::string strNodeName,
- std::map &AttMap)
- {
- typedef std::pair String_Pair;
-
- TiXmlDocument *pDoc = new TiXmlDocument();
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile);
- TiXmlElement *pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
-
- TiXmlElement *pNode = NULL;
- GetNodePointerByName(pRootEle,strNodeName,pNode);
- if (NULL!=pNode)
- {
- TiXmlAttribute* pAttr = NULL;
- std::string strAttName = _T("");
- std::string strAttValue = _T("");
- for (pAttr = pNode->FirstAttribute(); pAttr; pAttr = pAttr->Next())
- {
- strAttName = pAttr->Name();
- std::map::iterator iter;
- for (iter=AttMap.begin();iter!=AttMap.end();iter++)
- {
- if (strAttName==iter->first)
- {
- pAttr->SetValue(iter->second);
- }
- }
- }
- pDoc->SaveFile(XmlFile);
- return true;
- }
- else
- {
- return false;
- }
- }
对于ModifyNode_Attribute函数,这里稍微介绍一下如何使用,比如对于下面这样一个xml文件:
- xml version="1.0" encoding="utf-8" standalone="yes" ?>
- <MyApp>
- <Messages>
- <Welcome>Welcome to MyAppWelcome>
- <Farewell>Thank you for using MyAppFarewell>
- Messages>
- <Windows>
- <Window name="MainFrame" x="5" y="15" w="400" h="250" />
- Windows>
- <Connection ip="192.168.0.1" timeout="123.456000" />
- MyApp>
我们如果要修改节点的Connection的ip为192.168.0.100,timeout为1000,我们可以这样用:
- std::string XmlFile = _T("E://TestTinyxml//example4.xml");
- std::string strNodeName = _T("Connection");
- typedef std::pair String_Pair;
- std::map AttMap;
- AttMap.insert(String_Pair(_T("ip"),_T("192.168.0.100")));
- AttMap.insert(String_Pair(_T("timeout"),_T("1000")));
- ModifyNode_Attribute(XmlFile,strNodeName,AttMap);
下面是增加节点的两个函数:
-
-
-
-
-
-
-
-
-
- bool AddNode_Text(std::string XmlFile,std::string strParNodeName,std::string strNodeName,
- std::string strText)
- {
-
- TiXmlDocument *pDoc = new TiXmlDocument();
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile);
- TiXmlElement *pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement *pNode = NULL;
- GetNodePointerByName(pRootEle,strParNodeName,pNode);
- if (NULL!=pNode)
- {
-
- TiXmlElement *pNewNode = new TiXmlElement(strNodeName);
- if (NULL==pNewNode)
- {
- return false;
- }
-
- TiXmlText *pNewValue = new TiXmlText(strText);
- pNewNode->LinkEndChild(pNewValue);
- pNode->InsertEndChild(*pNewNode);
- pDoc->SaveFile(XmlFile);
- return true;
- }
- else
- return false;
-
- }
-
-
-
-
-
-
-
-
-
- bool AddNode_Attribute(std::string XmlFile,std::string strParNodeName,std::string strNodeName,
- std::map &AttMap)
- {
-
- TiXmlDocument *pDoc = new TiXmlDocument();
- if (NULL==pDoc)
- {
- return false;
- }
- pDoc->LoadFile(XmlFile);
- TiXmlElement *pRootEle = pDoc->RootElement();
- if (NULL==pRootEle)
- {
- return false;
- }
- TiXmlElement *pNode = NULL;
- GetNodePointerByName(pRootEle,strParNodeName,pNode);
- if (NULL!=pNode)
- {
-
- TiXmlElement *pNewNode = new TiXmlElement(strNodeName);
- if (NULL==pNewNode)
- {
- return false;
- }
-
- std::map::iterator iter;
- for (iter=AttMap.begin();iter!=AttMap.end();iter++)
- {
- pNewNode->SetAttribute(iter->first,iter->second);
- }
- pNode->InsertEndChild(*pNewNode);
- pDoc->SaveFile(XmlFile);
- return true;
- }
- else
- return false;
- }
阅读(1386) | 评论(0) | 转发(0) |