tinyxml语法讲解之写xml
生活随笔
收集整理的這篇文章主要介紹了
tinyxml语法讲解之写xml
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- TinyXml
- 簡介
- Qt+TinyXML 環境搭建
- 環境搭建
- TinyXML 框架解析
- DOM 對象模型
- 類圖關系
- 常用接口
- 寫 XML
TinyXml
簡介
TinyXML 是一個開源的解析 XML 的解析庫,能夠用于 C++,能夠在 Windows 或 Linux中編譯。這個解析庫的模型通過解析 XML 文件,然后在內存中生成 DOM(Document ObjectModel)模型,從而讓我們很方便的遍歷這棵 XML樹。
Qt+TinyXML 環境搭建
下載地址:
軟件下載:點擊此處下載軟件
官方文檔:點擊此處下載官方文檔
環境搭建
下載完成后,解壓找到 tinyxml2.cpp 以及 tinyxml2.h,并且添加到 c++工程目錄當中,與其他源碼一起編譯即可。
測試環境搭建:
#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; }運行結果:
TinyXML 框架解析
DOM 對象模型
TinyXml 使用文檔對象模型(DOM)來解析 xml 文件,這種模型的處理方式為在分析時,一次性的將整個 XML 文檔進行分析,并在內存中形成對應的樹結構,同時,向用戶提供一系列的接口來訪問和編輯該樹結構。這種方式占用內存大,但可以給用戶提供一個面向對象的訪問接口,對用戶更為友好,非常方便用戶使用。
類圖關系
namespace tinyxml2 {class XMLDocument;class XMLElement;class XMLAttribute;class XMLComment;class XMLText;class XMLDeclaration;class XMLUnknown;class XMLPrinter; }
發送xml格式數據如下:
我們把類圖和上面寫入的xml格式數據進行對應:
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
總結
以上是生活随笔為你收集整理的tinyxml语法讲解之写xml的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: operator new/delete,
- 下一篇: 什么是STL