TinyXml 修改指定節點和增加節點的做法

修改節點其實和查詢指定節點的值有點類似,也分爲兩個函數,一個實現修改文本。另一個負責修改屬性。

/*!
*  /brief 修改指定節點的文本。
*
*  /param XmlFile xml文件全路徑。
*  /param strNodeName 指定的節點名。
*  /param strText 重新設定的文本的值
*  /return 是否成功。true爲成功,false表示失敗。
*/
bool ModifyNode_Text(const std::string& XmlFile,const std::string& strNodeName,const std::string& strText)
{
   
// 定義一個TiXmlDocument類指針
    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;
}


/*!
*  /brief 修改指定節點的屬性值。
*
*  /param XmlFile xml文件全路徑。
*  /param strNodeName 指定的節點名。
*  /param AttMap 重新設定的屬性值,這是一個map,前一個爲屬性名,後一個爲屬性值
*  /return 是否成功。true爲成功,false表示失敗。
*/
bool ModifyNode_Attribute(const std::string& XmlFile,const std::string& strNodeName,
                
const std::map<std::string,std::string> &AttMap)
{
    typedef std::pair
<std::string,std::string> String_Pair;

   
// 定義一個TiXmlDocument類指針
    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
<std::string,std::string>::const_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 MyApp</Welcome>
       
<Farewell>Thank you for using MyApp</Farewell>
   
</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
<std::string,std::string> String_Pair;
   std::map
<std::string,std::string> 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);

下面是增加節點的兩個函數:

/*!
*  /brief 增加指定節點的文本。
*
*  /param XmlFile xml文件全路徑。
*  /param strParNodeName 要增加的節點的父節點。
*  /param strNodeName 指定的節點名。
*  /param strText 要增加的文本
*  /return 是否成功。true爲成功,false表示失敗。
*/
bool AddNode_Text(const std::string& XmlFile,const std::string& strParNodeName,const std::string& strNodeName,const std::string& strText)
{
   
// 定義一個TiXmlDocument類指針
    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)
    {
       
// 生成子節點:pNewNode
        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;
   
}

/*!
*  /brief 增加節點。
*
*  /param XmlFile xml文件全路徑。
*  /param strParNodeName 要增加的節點的父節點。
*  /param strNodeName 指定的節點名。
*  /param AttMap 要增加的節點設定的屬性值,這是一個map,前一個爲屬性名,後一個爲屬性值
*  /return 是否成功。true爲成功,false表示失敗。
*/
bool AddNode_Attribute(const std::string& XmlFile,const std::string& strParNodeName,const std::string strNodeName,const std::map<std::string,std::string> &AttMap)
{
   
// 定義一個TiXmlDocument類指針
    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)
    {
       
// 生成子節點:pNewNode
        TiXmlElement *pNewNode = new TiXmlElement(strNodeName);
       
if (NULL==pNewNode)
        {
           
return false;
        }
       
// 設置節點的屬性值,然後插入節點
        std::map<std::string,std::string>::const_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;
}
發佈了56 篇原創文章 · 獲贊 5 · 訪問量 81萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章