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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

使用Qt创建XML文档及XML文档的增删改

發布時間:2025/3/15 asp.net 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用Qt创建XML文档及XML文档的增删改 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄名字

  • 使用Qt創建XML文檔及XML文檔的增刪改
    • XML文檔的操作 :QXml + QFile+QTextStream
    • 創建XML的一般步驟:
    • XML 文檔的操作(添加、查找、更新、刪除)
      • 添加的一般步驟
      • 示例代碼:
      • 查找、更新、刪除的一般操作
    • 界面效果:
    • 總結:

使用Qt創建XML文檔及XML文檔的增刪改

XML文檔的操作 :QXml + QFile+QTextStream

創建XML的一般步驟:

1、添加處理指令及XML說明

2、添加元素

3、寫入文件

示例:

QDomDocument doc;// 添加處理指令即XML說明 QDomProcessingInstruction instruction; instruction = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\""); doc.appendChild(instruction);// 添加根元素 QDomElement root = doc.createElement(QString("書庫")); doc.appendChild(root);// 添加第一個圖書元素及其子元素 QDomElement book = doc.createElement(QString("圖書")); QDomAttr id = doc.createAttribute(QString("編號")); QDomElement title = doc.createElement(QString("書名")); QDomElement author = doc.createElement(QString("作者")); QDomText text;id.setValue(QString("1")); book.setAttributeNode(id); text = doc.createTextNode(QString("Qt")); title.appendChild(text); text = doc.createTextNode(QString("shiming")); author.appendChild(text); book.appendChild(title); book.appendChild(author); root.appendChild(book);// 添加第二個圖書元素及其子元素 book = doc.createElement(QString("圖書")); id = doc.createAttribute(QString("編號")); title = doc.createElement(QString("書名")); author = doc.createElement(QString("作者"));id.setValue(QString("2")); book.setAttributeNode(id); text = doc.createTextNode(QString("Linux")); title.appendChild(text); text = doc.createTextNode(QString("yafei")); author.appendChild(text); book.appendChild(title); book.appendChild(author); root.appendChild(book);QFile file("my.xml"); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return ; QTextStream out(&file); // 將文檔保存到文件,4為子元素縮進字符數 doc.save(out, 4); file.close();

細節:

  • 1、QDOMDocument類在內存中生成一顆DOM樹。
  • 2、createElement()添加元素 createAttribute()添加屬性 createTextNode()添加文本
    通過這些個函數來生成各種節點
  • 3、使用appendChild()將各個節點依次追加

XML 文檔的操作(添加、查找、更新、刪除)

添加的一般步驟

1、打開文件
2、QDomDocument 類來創建DOM樹
3、關閉文件
4、找到最后一個孩子節點的元素
5、新增元素,在最后一個孩子節點后追加一個新元素

示例代碼:

QFile file("my.xml");if (!file.open(QIODevice::ReadOnly)) return;QDomDocument doc;if (!doc.setContent(&file)){file.close();return;}file.close();QDomElement root = doc.documentElement();QDomElement book = doc.createElement(QString("圖書"));QDomAttr id = doc.createAttribute(QString("編號"));QDomElement title = doc.createElement(QString("書名"));QDomElement author = doc.createElement(QString("作者"));QDomText text;// 我們獲得了最后一個孩子結點的編號,然后加1,便是新的編號QString num = root.lastChild().toElement().attribute(QString("編號"));int count = num.toInt() +1;id.setValue(QString::number(count));book.setAttributeNode(id);text = doc.createTextNode(ui->lineEdit_2->text());title.appendChild(text);text = doc.createTextNode(ui->lineEdit_3->text());author.appendChild(text);book.appendChild(title);book.appendChild(author);root.appendChild(book);if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return ;QTextStream out(&file);doc.save(out, 4);file.close();

查找、更新、刪除的一般操作

1、elementByTagName()來獲取所有圖書元素的列表
2、使用指定的id編號來獲取要操作的圖書元素
3、刪除:removeChild()函數來刪除該元素并保存到文件。
4、更新:setNodeValue()來為其設置新的值并保存到文件
3、查找:將該元素的內容顯示出來。

// 先清空顯示 ui->listWidget->clear(); QFile file("my.xml"); if (!file.open(QIODevice::ReadOnly)) return ; QDomDocument doc; if (!doc.setContent(&file)) {file.close();return ; } file.close();QDomElement docElem = doc.documentElement();QDomNode n = docElem.firstChild(); while(!n.isNull()) {if (n.isElement()){QDomElement e = n.toElement();ui->listWidget->addItem(e.tagName() + e.attribute(QString("編號")));QDomNodeList list = e.childNodes();for(int i=0; i<list.count(); i++){QDomNode node = list.at(i);if(node.isElement())ui->listWidget->addItem(" " + node.toElement().tagName()+ " : " + node.toElement().text());}}n = n.nextSibling(); }

界面效果:

總結:

1、本篇的參考來源《Qt Creator 快速入門》
2、XML的操作,理解上并不復雜,操作上基本上按照流程來執行,但熟練掌握還需要勤加練習才好

總結

以上是生活随笔為你收集整理的使用Qt创建XML文档及XML文档的增删改的全部內容,希望文章能夠幫你解決所遇到的問題。

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