日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tinyxml语法讲解之写xml

發布時間:2025/3/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 version="1.0" encoding="utf-8"?> <!-- This is a XML comment -- > <bookstore><book category="COOKING"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price></book> </bookstore>

我們把類圖和上面寫入的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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。