Visual C++ TinyXml快速入門(三)

來源:互聯網
上載者:User

在《Visual C++ TinyXml快速入門(二)》介紹使用tinyxml庫擷取xml檔案聲明,查詢指定節點、刪除指定節點的做法。在本文中繼續介紹修改指定節點和增加節點的做法。

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

/*!
*  /brief 修改指定節點的文本。
*
*  /param XmlFile xml檔案全路徑。
*  /param strNodeName 指定的節點名。
*  /param strText 重新設定的文本的值
*  /return 是否成功。true為成功,false表示失敗。
*/
bool ModifyNode_Text(std::string XmlFile,std::string strNodeName,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(std::string XmlFile,std::string strNodeName,
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>::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(std::string XmlFile,std::string strParNodeName,std::string strNodeName,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(std::string XmlFile,std::string strParNodeName,std::string strNodeName,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>::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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.