TinyXml快速入門(一)_TinyXml

來源:互聯網
上載者:User

作者:朱金燦
來源:http://blog.csdn.net/clever101


      對於xml檔案,目前我的工作只是集中在設定檔和作為簡單的資訊檔來用,因此我不太喜歡使用msxml這種重量級的xml解析器,特別是使用msxml解析xml涉及到複雜的com類型轉換,更是令人感覺繁瑣。因此對於簡單的xml檔案的解析,我更願意使用開源的TinyXml。


      首先介紹一下TinyXml吧。TinyXML是目前非常流行的一款基於DOM模型的XML解析器,簡單易用且小巧玲瓏,非常適合儲存簡單資料,設定檔,對象序列化等資料量不是很大的操作,其首頁是:http://www.grinninglizard.com/tinyxml/ ,目前最新版本是2.5.3 版本。

TinyXml網上的教程很多,但是我覺得寫得都不怎樣(感覺就是看完之後就沒學會)。沒辦法,只得自己整理一篇適合自己的,至於適不適合別人,就見仁見智了。我感覺xml檔案本質就是小型的資料庫,換個角度來說就是,你對資料庫有什麼操作你對xml檔案就應能實現什麼操作。一般而言,對資料庫的操作包括以下幾種:建立資料庫、查詢資料庫、修改資料庫和刪除資料庫。那麼對應xml檔案就是建立xml檔案、查詢xml檔案的指定節點的值,修改xml檔案中節點的值和刪除xml檔案中節點的值。


       首先我們認識一下xml檔案有哪幾種形式。下面我列出一些常用的xml檔案的形式:


[xhtml]  view plain  copy example1.xml:   <?xml version="1.0" ?>   <Hello>World</Hello>   example2.xml:   <?xml version="1.0" ?>   <poetry>          <verse>                  Alas                    Great World                          Alas (again)          </verse>   </poetry>   example3.xml:   <?xml version="1.0" ?>   <shapes>          <circle name="int-based" x="20" y="30" r="50" />          <point name="float-based" x="3.5" y="52.1" />   </shapes>   example4.xml:   <?xml version="1.0" ?>   <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>  


      上面的例子摘自《TinyXML Tutorial 中文指南》。上面有四個例子,你看到了xml檔案的幾種表現形式。我看到了本質來說不過是兩種表現形式:屬性值值在角括弧內,如<Window name="MainFrame" x="5" y="15" w="400" h="250" />和文本在角括弧外,如<Welcome>Welcome to MyApp</Welcome>,具體如下圖:



     鑒於example4.xml比較複雜,下面我將以此為例介紹tinyxml的使用。


     Tinyxml使用了兩種編譯選擇:使用標準C的char *類型或者使用STL中的std::string,其中使用前置處理器TIXML_USE_STL進行控制,即添加了TIXML_USE_STL為使用std::string的。鑒於STL的廣泛使用以及其強大功能,下面我以使用std::string的tinyxml說明。

首先使用VS 2005開啟tinyxmlSTL.dsp的工程檔案,將其編譯成一個靜態庫,debug版本為:tinyxmld_STL.lib,然後開始測試tinyxml庫。我的測試計劃是這樣的:首先使用tinyxml庫建立example4.xml,然後將其讀出來,然後查詢指定節點的屬性或文本,再修改example4.xml(修改其中的一些節點值和刪除其中一個節點,增加一個節點),然後再讀出來以判斷是否修改成功。具體是在VS 2005上建立一個控制台工程:Test,注意使用多位元組字元集進行編譯,同時添加。首先是建立xml檔案的代碼:


     [cpp]  view plain  copy /*!  *  /brief 建立xml檔案。  *  *  /param XmlFile xml檔案全路徑。  *  /return 是否成功。true為成功,false表示失敗。  */   bool CreateXml(std::string XmlFile)   {       // 定義一個TiXmlDocument類指標       TiXmlDocument *pDoc = new TiXmlDocument;       if (NULL==pDoc)       {           return false;       }       TiXmlDeclaration *pDeclaration = new TiXmlDeclaration(_T("1.0"),_T(""),_T(""));       if (NULL==pDeclaration)       {           return false;       }       pDoc->LinkEndChild(pDeclaration);       // 產生一個根節點:MyApp       TiXmlElement *pRootEle = new TiXmlElement(_T("MyApp"));       if (NULL==pRootEle)       {           return false;       }       pDoc->LinkEndChild(pRootEle);       // 產生子節點:Messages       TiXmlElement *pMsg = new TiXmlElement(_T("Messages"));       if (NULL==pMsg)       {           return false;       }       pRootEle->LinkEndChild(pMsg);       // 產生子節點:Welcome       TiXmlElement *pWelcome = new TiXmlElement(_T("Welcome"));       if (NULL==pWelcome)       {           return false;       }       pMsg->LinkEndChild(pWelcome);       // 設定Welcome節點的值       std::string strValue = _T("Welcome to MyApp");       TiXmlText *pWelcomeValue = new TiXmlText(strValue);       pWelcome->LinkEndChild(pWelcomeValue);       // 產生子節點:Farewell       TiXmlElement *pFarewell = new TiXmlElement(_T("Farewell"));       if (NULL==pFarewell)       {           return false;       }       pMsg->LinkEndChild(pFarewell);       // 設定Farewell節點的值       strValue = _T("Thank you for using MyApp");       TiXmlText *pFarewellValue = new TiXmlText(strValue);       pFarewell->LinkEndChild(pFarewellValue);       // 產生子節點:Windows       TiXmlElement *pWindows = new TiXmlElement(_T("Windows"));       if (NULL==pWindows)       {           return false;       }       pRootEle->LinkEndChild(pWindows);       // 產生子節點:Window       TiXmlElement *pWindow = new TiXmlElement(_T("Window"));       if (NULL==pWindow)       {           return false;       }       pWindows->LinkEndChild(pWindow);       // 設定節點Window的值       pWindow->SetAttribute(_T("name"),_T("MainFrame"));       pWindow->SetAttribute(_T("x"),_T("5"));       pWindow->SetAttribute(_T("y"),_T("15"));       pWindow->SetAttribute(_T("w"),_T("400"));       pWindow->SetAttribute(_T("h"),_T("250"));       // 產生子節點:Window       TiXmlElement *pConnection  = new TiXmlElement(_T("Connection"));       if (NULL==pConnection)       {           return false;       }       pRootEle->LinkEndChild(pConnection);       // 設定節點Connection的值       pConnection->SetAttribute(_T("ip"),_T("192.168.0.1"));       pConnection->SetAttribute(_T("timeout"),_T("123.456000"));       pDoc->SaveFile(XmlFile);       return true;   }   



       不知你注意到上面的規律沒有。首先父節點串連位元組點使用函數LinkEndChild,使用方法是:pParentNode-> LinkEndChild(pChild);其次設定類似這種結構<Window name="MainFrame" x="5" y="15" w="400" h="250" />採用SetAttribute函數,這個函數有兩個參數,前一個參數表示鍵,後一個參數表示索引值,設定<Farewell>Thank you for using MyApp</Farewell>這種結構採用TiXmlText類,使用LinkEndChild函數進行連結。


      上面是建立xml檔案的代碼,下面介紹讀取xml檔案的代碼。列印整個xml檔案的代碼很簡單,代碼如下:


[cpp]  view plain  copy /*!  *  /brief 列印xml檔案。  *  *  /param XmlFile xml檔案全路徑。  *  /return 是否成功。true為成功,false表示失敗。  */   bool PaintXml(std::string XmlFile)   {       // 定義一個TiXmlDocument類指標       TiXmlDocument *pDoc = new TiXmlDocument();       if (NULL==pDoc)       {           return false;       }       pDoc->LoadFile(XmlFile);       pDoc->Print();       return true;   }  



   下次介紹使用tinyxml庫對xml檔案進行查詢指定節點、刪除指定節點、修改指定節點和增加節點的用法。

參考文獻:


1.《TinyXML入門教程 》


2. 《tinyxml 使用筆記與總結 》


3. 《TinyXML Tutorial 中文指南 》

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.