TinyXml高速入門(一)

來源:互聯網
上載者:User

標籤:style   blog   http   使用   io   檔案   for   資料   ar   

朱金燦
來源: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檔案的形式:


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的project檔案,將其編譯成一個靜態庫,debug版本號碼為:tinyxmld_STL.lib,然後開始測試tinyxml庫。我的測試計劃是這種:首先使用tinyxml庫建立example4.xml,然後將其讀出來,然後查詢指定節點的屬性或文本,再改動example4.xml(改動當中的一些節點值和刪除當中一個節點,添加一個節點),然後再讀出來以推斷是否改動成功。詳細是在VS 2005上建立一個控制台project:Test,注意使用多位元組字元集進行編譯,同一時候加入。首先是建立xml檔案的代碼:


     /*!* /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);// 產生一個根節點:MyAppTiXmlElement *pRootEle = new TiXmlElement(_T("MyApp"));if (NULL==pRootEle){return false;}pDoc->LinkEndChild(pRootEle);// 產生子節點:MessagesTiXmlElement *pMsg = new TiXmlElement(_T("Messages"));if (NULL==pMsg){return false;}pRootEle->LinkEndChild(pMsg);// 產生子節點:WelcomeTiXmlElement *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);// 產生子節點:FarewellTiXmlElement *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);// 產生子節點:WindowsTiXmlElement *pWindows = new TiXmlElement(_T("Windows"));if (NULL==pWindows){return false;}pRootEle->LinkEndChild(pWindows);// 產生子節點:WindowTiXmlElement *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"));// 產生子節點:WindowTiXmlElement *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檔案的代碼非常easy,代碼例如以下:


/*!* /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 中文指南》

















TinyXml高速入門(一)

聯繫我們

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