使用Qt創建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的操作,理解上並不複雜,操作上基本上按照流程來執行,但熟練掌握還需要勤加練習纔好

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章