1. 程式人生 > >TinyXML讀取XML檔案內容 [大三四八九月實習]

TinyXML讀取XML檔案內容 [大三四八九月實習]

1 XML檔案的節點與元素

為什麼要探討這個問題呢,因為在TinyXML類的成員函式中,有指向下一個結點的成員函式(NextSibling)有指向下一個元素的成員函式(NextSiblingElement),元素在XML檔案基本結構中基本已經形成概念,現在又冒出一個結點。還有的就是我們要訪問的內容在TinyXML類中叫什麼名字,用什麼方法來訪問?這個都是需要驗證一下子的。

關於節點和元素的問題還是藉助於[http://www.w3school.com.cn/xmldom/dom_nodes.asp]文章[ 不要細看XML程式碼,看程式碼後面的解說 ]:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book category="children">
  <title lang="en">Harry Potter</title> 
  <author>J K. Rowling</author> 
  <year>2005</year> 
  <price>29.99</price> 
</book>

<book category="cooking">
  <title lang="en">Everyday Italian</title> 
  <author>Giada De Laurentiis</author> 
  <year>2005</year> 
  <price>30.00</price> 
</book>

<book category="web">
  <title lang="en">Learning XML</title> 
  <author>Erik T. Ray</author> 
  <year>2003</year> 
  <price>39.95</price> 
</book>

<book category="web">
  <title lang="en">XQuery Kick Start</title> 
  <author>James McGovern</author> 
  <author>Per Bothner</author> 
  <author>Kurt Cagle</author> 
  <author>James Linn</author> 
  <author>Vaidyanathan Nagarajan</author> 
  <year>2003</year> 
  <price>49.99</price> 
</book>

</bookstore>


在上面的 XML 中,根節點是 <bookstore>。文件中的所有其他節點都被包含在 <bookstore>中。

根節點 <bookstore>有四個 <book>節點。

第一個<book>節點有四個節點:<title>, <author>, <year>以及<price>,其中每個節點都包含一個文字節點,"Harry Potter", "J K. Rowling", "2005"以及 "29.99"

文字總是儲存在文字節點中

DOM處理中一個普遍的錯誤是,認為元素節點包含文字。

不過,元素節點的文字是儲存在文字節點中的。

在這個例子中:<year>2005</year>,元素節點 <year>,擁有一個值為"2005"的文字節點。

"2005"不是 <year> 元素的值!

2 TinyXML讀取XML檔案中的內容例項

(1)總結

驗證一下XML檔案中的每個部分到底叫什麼名字,在TinyXML中用什麼方法訪問,及每個函式對應的XML檔案結構。

Figure1:簡單的XML與TinyXML方法對應

經過程式驗證,得出以上XML檔案對應TinyXML中的方法為:

[1] <ChildStation>:是整個文件的根元素(也是根節點),用TiXmlDocument:: RootElement()

方法得到指向此元素(節點的指標),得到指向此根元素的指標之後,<ChildStation>中的“ChildStation”叫做Value,可以用TiXmlDocument:: RootElement()->Value();將其輸出到螢幕中。

[2]<Name> …</Name><Address>…</Address >屬於<ChildStation>的子元素(子結點),可以通過根元素利用FirstChildElement()獲得]<ChildStation>下的第一個子元素,然後通過此子元素用NextSibliingElement()方法獲得下一個跟<Name>處於同一級別( 屬於<ChildStation>第二個子元素)的指標,一次類推。獲取每個元素的文字內容用的是GetText()方法,通過此方法就能夠獲得以上“STM100”、“0001”等內容。

這樣就能夠讀取每個元素下“<>”及“<>”與“</>”之間的內容。

對於層次一樣的元素點,用NextSibliingElement()方法獲取下一個元素點,如果是獲取此元素的子元素點,則利用FirstChildElement()方法先獲取第一個子元素點,子元素點之間再用NextSibliingElement()方法來獲取下一個同層次的元素點。

只要訪問到一個元素“<>…</>”利用[1][2]就可以訪問到所有的內容,似乎足矣。

(2)讀取例項

針對以上的XML文字,可以用以上總結的[1][2]兩點將ValueText讀取出來:

void MyTinyXML::select_node( )
{
    //Get the root element
	pDoc->LoadFile();
	pRootElement = pDoc->RootElement();
	cout << " \nroot value:" << pRootElement->Value();
	cout << " \nroot text:" << pRootElement->GetText();
	
	TiXmlElement *pCurrentElement = NULL;

	//Go in to root's child element
	pCurrentElement = pRootElement->FirstChildElement();
	while( pCurrentElement )
	{
	       //Print value
	       cout << "\child value:"<< pCurrentElement->Value();
	       //Print text
	       //cout << "\child text:"<< pCurrentElement->GetText();
	     

	       //Next child element
	       pCurrentElement = pCurrentElement->NextSiblingElement();

	}
	
	if ( pCurrentElement )
	{
	    delete pCurrentElement;
	    pCurrentElement = NULL;
	}
	
}

說明:不見定義的都是類中的私有成員。

對於XML中沒有的內容就不要用TinyXML程式碼讀取了,不然會有意外的錯誤。

讀取結果:

Figure2:讀取的XML檔案內容

此次筆記記錄完畢。