Google

星期三, 四月 16, 2008

TinyXML与wxWidgets

TinyXML 是一個小巧的 C++ XML parser Library,十分容易就可以整合到你的程式,而且學習曲線十分的短。

TiXmlNode

TinyXML 使用 node 的觀念來看待 XML 文件樹,TiXmlNode 就是代表著一個節點的 class。

Load and Save

TinyXML 使用 TiXmlDocument 這個 class 來載入/儲存 xml file。A document binds together all the XML pieces. It can be saved, loaded, and printed to the screen.

TiXmlDocument doc;doc.Load( "myfile.xml" );

存取的時候,要先取得下一層的 Child Element:

TiXmlElement* element = doc.FirstChildElement();

也可以指定標籤來取得特定的 Element,如同下面的例子:

TiXmlElement* element = doc.FirstChildElement("Node");

TiXmlElement

The element is a container class. It has a value, the element name, and can contain other elements, text, comments, and unknowns. Elements also contain an arbitrary number of attributes.
<foo>This is textfoo>

例如上面的片斷,我們可以使用 GetText() 取得被包起來的文字:
const char* str = fooElement->GetText();

TinyXML and wxWidgets

TinyXML 預設以 UTF-8 編碼,wxWidgets 編譯成 Unicode mode 時,傳入的參數需要轉碼,wxString 可以使用 mb_str() 轉碼,就可以與 TinyXML一起使用。

如果要轉換 UTF-8 char * 字串為 wxString,使用下列的方式:

const char* str = fooElement->GetText();wxString myString(str, wxConvUTF8);
http://www.cppblog.com/tx7do/archive/2006/04/24/6153.html

标签: , ,

辽ICP备05003652号
流风洄雪听天籁,轻云蔽日看落花

Powered by Blogger