(该例子参考《Qt Creator快速入门 第三版》src\17\17-10)
1、pro中添加如下代码
QT += xml
2、UI界面设计如下图所示
3、代码部分
#ifndef MAINWINDOW_H #define MAINWINDOW_H #if _MSC_VER >=1600 // MSVC2015>1899,对于MSVC2010以上版本都可以使用 #pragma execution_character_set("utf-8") #endif #include <QMainWindow> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); void doXml(const QString operate); // 用于功能判断 private slots: void on_showBtn_clicked(); void on_addBtn_clicked(); void on_deleteBtn_clicked(); void on_updateBtn_clicked(); void on_findBtn_clicked(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
#include "mainwindow.h"#include "ui_mainwindow.h"#include<QtXml>#include<QFile>
#if _MSC_VER >=1600 // MSVC2015>1899,对于MSVC2010以上版本都可以使用 #pragma execution_character_set("utf-8") #endif MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QDomDocument doc; QDomProcessingInstruction instruction; instruction = doc.createProcessingInstruction("xml", "version = \"1.0\" encoding = \"UTF-8\""); doc.appendChild(instruction); // 添加根元素 QDomElement root = doc.createElement("library"); doc.appendChild(root); QDomElement book = doc.createElement("book"); QDomAttr id = doc.createAttribute("id"); QDomElement title = doc.createElement("name"); QDomElement author = doc.createElement("author"); QDomText text; id.setValue(QString("1")); book.setAttributeNode(id); text = doc.createTextNode("Qt"); title.appendChild(text); text = doc.createTextNode("shiming"); author.appendChild(text); book.appendChild(title); book.appendChild(author); root.appendChild(book); book = doc.createElement("book"); id = doc.createAttribute("id"); title = doc.createElement("name"); author = doc.createElement("author"); id.setValue(QString("2")); book.setAttributeNode(id); text = doc.createTextNode("Linux"); title.appendChild(text); text = doc.createTextNode("zk"); 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); doc.save(out, 4); file.close(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::doXml(const QString operate) { ui->listWidget->clear(); ui->listWidget->addItem("没有找到相关内容"); QFile file("my.xml"); if(!file.open(QIODevice::ReadOnly)) return ; QDomDocument doc; if(!doc.setContent(&file)) { file.close(); return ; } file.close(); QDomNodeList list = doc.elementsByTagName("book"); for (int i = 0; i < list.count(); i++) { QDomElement e = list.at(i).toElement();
if(e.attribute("id") == ui->lineEdit->text()) // 判断节点 { if(operate == "delete") { QDomElement root = doc.documentElement(); root.removeChild(list.at(i)); QFile file("my.xml"); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return; QTextStream out(&file); doc.save(out, 4); file.close(); ui->listWidget->clear(); ui->listWidget->addItem("删除成功~"); } else if(operate == "update") // 更新节点内容 { QDomNodeList child = list.at(i).childNodes(); child.at(0).toElement().firstChild().setNodeValue( ui->lineEdit_2->text()); child.at(1).toElement().firstChild().setNodeValue( ui->lineEdit_3->text()); QFile file("my.xml"); if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate)) return; QTextStream out(&file); doc.save(out, 4); file.close(); ui->listWidget->clear(); ui->listWidget->addItem("更新成功~"); } else if(operate == "find") { ui->listWidget->clear(); ui->listWidget->addItem(e.tagName() + e.attribute("id")); 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()); } } } } } void MainWindow::on_showBtn_clicked() { 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(); ui->listWidget->addItem(docElem.tagName()); QDomNode n = docElem.firstChild(); while (!n.isNull()) { if(n.isElement()) { QDomElement e = n.toElement(); ui->listWidget->addItem(" " + e.tagName() + e.attribute("id")); 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(); } } void MainWindow::on_addBtn_clicked() { ui->listWidget->clear(); ui->listWidget->addItem("无法添加..."); 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("book"); QDomAttr id = doc.createAttribute("id"); QDomElement title = doc.createElement("name"); QDomElement author = doc.createElement("author"); QDomText text; QString num = root.lastChild().toElement().attribute("id"); 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(); ui->listWidget->clear(); ui->listWidget->addItem("添加成功~"); } void MainWindow::on_deleteBtn_clicked() { doXml("delete"); } void MainWindow::on_updateBtn_clicked() { doXml("update"); } void MainWindow::on_findBtn_clicked() { doXml("find"); }