TiXml的使用方法

2021-09-25 04:49:07 字數 4234 閱讀 1099

本文用乙個詳細的例子說明了tixml的使用方法。如寫、查詢、插入、替換、載入、遍歷等常見操作。

首先簡單介紹一下tinyxml,要看詳細的在網上搜搜了^_^:

tinyxml是乙個簡單小巧,可以很容易整合到其它程式中的c++ xml解析器。簡單地說,tinyxml解析乙個xml文件並由此生成乙個可讀可修改可儲存的文件物件模型(dom)。tinyxml使用文件物件模型(dom),這意味著xml資料被解析成乙個可被瀏覽和操作的c++物件,然後它可以被寫到磁碟或者另乙個輸出流中。你也可以把c++物件構造成乙個xml文件然後把它寫到磁碟或者另乙個輸出流中。

1、tinyxml源**只有4個cpp檔案和2個頭檔案。

2、首先要理解tinyxml中的各個基本型別之間的關係,看看這個繼承圖大家就會很明白了!

可以看到tinyxml中的注釋comment ,宣告declaration,元素element,文字等都是節點node的子類,也就是說可以把xml檔案中的各個元素當做節點來處理。node型別也有到各個子類之間的轉換方法,如toelement()轉換成元素,todocument轉換成文件等。

因此可以吧tixmlnode作為tinyxml的基本資料型別來操作,這樣轉化到其他型別也比較方便!

3、類之間的關係

tinyxml實現的是dom訪問模型,因此提供了一系列的類對應xml檔案中的各個節點。主要類間的關係如下:

tixmlbase:其他類的基類,是個抽象類

tixmlnode:表示乙個節點,包含節點的一般方法,如訪問子節點、兄弟節點、編輯自身、編輯子節點

tixmldocument:表示整個xml文件,不對應其中某個特定的節點。

tixmlelement:表示元素節點,可以包含子節點和tixmlattribute

tixmlcomment:表示注釋

tixmldeclaration:表示宣告

tixmltext:表示文字節點

tixmlunknown:表示未知節點,通常是出錯了

tixmlattribute:表示乙個元素的屬性

下面是乙個簡單的例子:

<?xml version="1.0" encoding="utf-8" ?>

tinyxml how to

20some words…

整個文件,對應tixmldocument

book,name,price, description,都對應tixmlelement

第一行對應乙個tixmldeclaration

第二行對應乙個tixmlcomment

「tinyxml how to」對應乙個tixmltext

unit則是price的乙個tixmlattribute

這些類與xml檔案中的相應元素都有很好的對應關係,因此相信參照tinyxml的文件,可以很容易的掌握各個方法的使用。

2. 需要注意的問題

各類之間的轉換

由於各個節點類都從tixmlnode繼承,在使用時常常需要將tixmlnode*型別的指標轉換為其派生類的指標,在進行這種轉換時,應該首先使用由tixmlnode類提供的一系列轉換函式,如toelement(void),而不是c++的dynamic_cast。

檢查返回值

由於tinyxml是乙個非校驗的解析器,因此當解析乙個檔案時,很可能檔案並不包含我們預期的某個節點,在這種情況下,tinyxml將返回空指標。因此,必須要對返回值進行檢查,否則將很容易出現記憶體訪問的錯誤。

如何重頭建立乙個xml檔案

先建立乙個tixmldocument物件,然後,載入某個模板,或者直接插入乙個節點作為根節點,接著就可以像開啟乙個已有的xml檔案那樣對它進行操作了。

4、要理解tinyxml中的每個節點都可能是另乙個節點的父節點這個很重要,因此遍歷tinyxml文件要用遞迴的方法。每個節點都可能有屬性,文字什麼的!

5、每個type of tixmlnode節點的值'value'對應如下 :

document: filename of the xml file

element: name of the element

comment: the comment text

unknown: the tag contents

tixmldocument *pdoc =null;

void write_xml( )

void dump_to_stdout( tixmlnode* pparent )//tixml主頁上給的乙個遍歷方法(遞迴呼叫)

break;

case tixmlnode::comment:

printf( "comment: [%s]", pparent->value());

break;

case tixmlnode::unknown:

printf( "unknown" );

break;

case tixmlnode::text:

ptext = pparent->totext();

printf( "text: [%s]", ptext->value() );

break;

case tixmlnode::declaration:

printf( "declaration" );

break;

default:

break;

}printf( "/n" );

for ( pchild = pparent->firstchild(); pchild != 0; pchild = pchild->nextsibling())

}void search(tixmlnode* pparent)//遍歷時候,把每個節點都是做乙個父節點,即假定其都有子節點childnode

printf( "/n" );

for ( pchild = pparent->firstchild(); pchild != 0; pchild = pchild->nextsibling()) }

int main(int argc, char* ar**)

else

return 0;

}9、引例

#include

#include"tinyxml.h"

#include"tinystr.h"

#include

#include

#include

usingnamespacestd;

tchar modulepath[max_path];

getmodulefilename(null, modulepath, max_path);

cstring strmodulepath(modulepath);

strmodulepath = strmodulepath.left(strmodulepath.reversefind(_t('\\')));

return strmodulepath;

}bool createxmlfile(string& szfilename)

catch(string& e)

returntrue;

}bool readxmlfile(string& szfilename)

{//讀取xml檔案,並遍歷

tixmldocument *mydocument =new tixmldocument(fullpath.c_str());

mydocument->loadfile();

//獲得根元素,即persons。

tixmlelement *rootelement = mydocument->rootelement();

//輸出根元素名稱,即輸出persons。

cout<< rootelement->value()firstchildelement();

//獲得第乙個person的name節點和age節點和id屬性。

tixmlelement *nameelement = firstperson->firstchildelement();

tixmlelement *ageelement = nameelement->nextsiblingelement();

tixmlattribute *idattribute = firstperson->firstattribute();

//輸出第乙個person的name內容,即周星星;age內容,即;id屬性,即。

cout<< nameelement->firstchild()->value()value()

TiXml使用詳解

本文用乙個詳細的例子說明了tixml的使用方法。如寫 查詢 插入 替換 載入 遍歷等常見操作。首先簡單介紹一下tinyxml,要看詳細的在網上搜搜了 tinyxml是乙個簡單小巧,可以很容易整合到其它程式中的c xml解析器。簡單地說,tinyxml解析乙個xml文件並由此生成乙個可讀可修改可儲存的...

TiXml使用詳解

tixml使用詳解 本文用乙個詳細的例子說明了tixml的使用方法。如寫 查詢 插入 替換 載入 遍歷等常見操作。首先簡單介紹一下tinyxml,要看詳細的在網上搜搜了 tinyxml是乙個簡單小巧,可以很容易整合到其它程式中的c xml解析器。簡單地說,tinyxml解析乙個xml文件並由此生成乙...

TiXml使用介紹

tixml使用詳解 本文用乙個詳細的例子說明了tixml的使用方法。如寫 查詢 插入 替換 載入 遍歷等常見操作。tinyxml是乙個簡單小巧,可以很容易整合到其它程式中的c xml解析器。簡單地說,tinyxml解析乙個xml文件並由此生成乙個可讀可修改可儲存的文件物件模型 dom tinyxml...