tinyxml语法讲解之写xml
- TinyXml
- 簡(jiǎn)介
- Qt+TinyXML 環(huán)境搭建
- 環(huán)境搭建
- TinyXML 框架解析
- DOM 對(duì)象模型
- 類圖關(guān)系
- 常用接口
- 寫 XML
TinyXml
簡(jiǎn)介
TinyXML 是一個(gè)開源的解析 XML 的解析庫(kù),能夠用于 C++,能夠在 Windows 或 Linux中編譯。這個(gè)解析庫(kù)的模型通過解析 XML 文件,然后在內(nèi)存中生成 DOM(Document ObjectModel)模型,從而讓我們很方便的遍歷這棵 XML樹。
Qt+TinyXML 環(huán)境搭建
下載地址:
軟件下載:點(diǎn)擊此處下載軟件
官方文檔:點(diǎn)擊此處下載官方文檔
環(huán)境搭建
下載完成后,解壓找到 tinyxml2.cpp 以及 tinyxml2.h,并且添加到 c++工程目錄當(dāng)中,與其他源碼一起編譯即可。
測(cè)試環(huán)境搭建:
#include <iostream> #include "tinyxml2.h" using namespace tinyxml2; using namespace std; int main() {XMLDocument doc;doc.LoadFile("baidu.xml");doc.Print();cout << "doc status " << doc.ErrorName() << endl;return 0; }運(yùn)行結(jié)果:
TinyXML 框架解析
DOM 對(duì)象模型
TinyXml 使用文檔對(duì)象模型(DOM)來解析 xml 文件,這種模型的處理方式為在分析時(shí),一次性的將整個(gè) XML 文檔進(jìn)行分析,并在內(nèi)存中形成對(duì)應(yīng)的樹結(jié)構(gòu),同時(shí),向用戶提供一系列的接口來訪問和編輯該樹結(jié)構(gòu)。這種方式占用內(nèi)存大,但可以給用戶提供一個(gè)面向?qū)ο蟮脑L問接口,對(duì)用戶更為友好,非常方便用戶使用。
類圖關(guān)系
namespace tinyxml2 {class XMLDocument;class XMLElement;class XMLAttribute;class XMLComment;class XMLText;class XMLDeclaration;class XMLUnknown;class XMLPrinter; }
發(fā)送xml格式數(shù)據(jù)如下:
我們把類圖和上面寫入的xml格式數(shù)據(jù)進(jìn)行對(duì)應(yīng):
namespace tinyxml2 {class XMLDocument; //xxx.xmlclass XMLElement; //<price>30.00</price>class XMLAttribute; //category="COOKING"class XMLComment; //<!-- This is a XML comment -- >class XMLText; //Giada De Laurentiisclass XMLDeclaration; //<?xml version="1.0" encoding="utf-8"?>class XMLUnknown; class XMLPrinter; }常用接口
XMLElement* RootElement() XMLElement* FirstChildElement( const char* name = 0 ) XMLElement*PreviousSiblingElement( const char* name = 0 ) XMLElement* NextSiblingElement( const char* name = 0 ) XMLNode* LinkEndChild( XMLNode* addThis ) XMLElement* XMLNode::FirstChildElement( const char* name ) XMLElement* XMLNode::LastChildElement( const char* name ) XMLElement* XMLDocument::NewElement( const char* name ) XMLComment* XMLDocument::NewComment( const char* str ) XMLText* XMLDocument::NewText( const char* str ) XMLDeclaration* XMLDocument::NewDeclaration( const char* str ) const char* XMLAttribute::Name() const寫 XML
總結(jié)
以上是生活随笔為你收集整理的tinyxml语法讲解之写xml的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: operator new/delete,
- 下一篇: C++析构器详解【C++析构器】