XML解析器 TinyXML 的使用

2021-04-13 08:47:00 字數 3274 閱讀 8769

xml解析器(tinyxml)的使用

xml

解析器(tinyxml)

的使用

參考http://www.cppblog.com/dyj057/archive/2007/01/26/18007.html

我是在vc下編譯執行。

1.tinyxml

庫的檔案

,這裡給出鏈結

2.,把所有的東西放到乙個找的著的地方(比如

,e:/

開發庫/tinyxml)

3.用visual c++(

推薦vc++.net2003)

建立乙個新的工程

(win32

控制台)

4.在tinyxml

的目錄裡面找到

tinystr.h, tinyxml.h, tinystr.cpp, tinyxml.cpp, tinyxmlerror.cpp, tinyxmlparser.cpp

六個檔案加入到剛剛建立的專案中去

5.開啟tinyxml.h, 

在第一行加入下面這行

:#define tixml_use_stl //

標誌使用

stl的內容

6.然後建立乙個

cpp檔案

,輸入下面的內容:

#include

"stdafx.h"

#include

#include

#include

"tinyxml.h"

using

namespace std;

int_tmain(int argc, _tchar* argv)

*p = 0;

ifs.close();

//

//這裡開始從字串中解析xml

//建立tixmldocument物件

tixmldocument* doc = new tixmldocument();

//解析

if(!doc->parse(buffer))

//獲取根節點

const

tixmlelement* root = doc->rootelement();

//迴圈獲取該根節點下面的節點

for( const tixmlnode* child = root->firstchild();

child;

child=child->nextsibling())

else

if(!strcmp(sub_tag->value(),"dimension"))

else

if(!strcmp(sub_tag->value(),"test"))

}}

cout << "/n";

cout << "/tposition = ("

<< px << ", "

<< py << ", "

<< pz << ")/n";

cout << "/tdimension = ("

<< dx << ", "

<< dy << ", "

<< dz << ")/n/n"; }

}

delete

doc;

getchar();

return 0; }

7.然後在專案的資料夾中加入乙個

xml檔案,如下

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

8.編譯執行

附另乙個tinyxml讀寫xml的例子,此處寫xml是直接寫入xml檔案,如果要使用這裡的函式不寫入檔案而是組織成xml流,需要相應修改**。**在vs2005中測試執行成功。

#define

tixml_use_stl //

標誌使用stl的內容

#include

"stdafx.h"

#include

#include

#include

#include

"tinyxml.h"

using

namespace std;

intwritexml() ;

intreadxml() ;

//讀取記憶體裡的一段xml

intreadxml2() ;

int_tmain(int argc, _tchar* argv)

intwritexml()

intreadxml()

else

cout<<"

結束read xml"

<

return 0 ; }

intreadxml2()

else

cout<<"

結束read xml"

<

return 0 ; }

//----------------------------mycode--------------------------------//

private:

template

static t * _tinyxmlparser(const tixmlelement *_element,t *_fieldsptr);

public:

static  void * xmlparser(const string &xmlstream,int layers=1);

//---------------------------------cpp----------------------------------//

template

t * util::_tinyxmlparser(const tixmlelement *_element,t *_fieldsptr)

else

}return _fieldsptr;

}void * util::xmlparser(const std::string &xmlstream,int layers)

//獲取根節點

const tixmlelement* root = doc.rootelement();

switch(layers)

break;

default:

break;

} }

catch(...)

}

pull解析器解析xml

利用pull解析xml檔案需要下面幾個步驟 1 獲取xmlpullparser物件。這裡有兩個方法 通過xmlpullparse ctory獲取xmlpullparser物件,或者直接使用xml.newpullparser 方法獲取。栗子如 一所示。2 通過xmlpullparser物件設定輸入流。...

XML解析 Jsoup解析器

jsoup快捷查詢方式 jsoup概念 跳轉到目錄 jsoup基本使用 提取碼 0uvi 獲取document物件 獲取對應標籤的element物件 獲取資料 public static void getfirstname throws exception jsoup中的物件 跳轉到目錄 獲取對應的...

JAL的XML解析器

基於sax的,試了一下,相當好用 procedure tform1.domview document idocument view ttreeview procedure par mlnode datanode ielement treenode ttreenode vari iiterator n...