找回密码
 立即注册
收起左侧

请问对这三个类的用法有比较熟悉QDomDocument,QDomElement,QDo...

9
回复
7365
查看
[复制链接]
累计签到:14 天
连续签到:1 天
来源: 2016-3-4 17:41:36 显示全部楼层 |阅读模式
1Qter豆
请帮忙介绍介绍这三个类的功能,和用法。
还有什么是XML,查了一些资料感觉有些晕
#include <QDomDocument>
#include <QDomElement>
#include <QDomNode>


最佳答案

查看完整内容

Qt shiming Linux yafei
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-4 17:41:37 显示全部楼层
<?xml version="1.0" encoding="UTF-8"?>
<library>
    <book id="01">
        <title>Qt</title>
        <author>shiming</author>
    </book>
    <book id="02">
        <title>Linux</title>
        <author>yafei</author>
    </book>
</library>
回复

使用道具 举报

累计签到:410 天
连续签到:1 天
2016-3-4 18:24:21 显示全部楼层
本帖最后由 a408815041 于 2016-3-4 18:26 编辑

XML 就是  数据存储语言。

看最后个单词也知道大致什么意思,, 文档,元素,结点

你实在不知道这些类的功能,,你去助手里面看啊,,写的很详细。
看不懂英文,,自己去百度翻译或者 下载个有道词典,,虽然不是100%翻译对,,大致也能猜到
比如
Detailed Description

The QDomDocument class represents an XML document.

The QDomDocument class represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document, the document class also contains the factory functions needed to create these objects. The node objects created have an ownerDocument() function which associates them with the document within whose context they were created. The DOM classes that will be used most often are QDomNode, QDomDocument, QDomElement and QDomText.

The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them or the QDomDocument itself is deleted.

Creation of elements, text nodes, etc. is done using the various factory functions provided in this class. Using the default constructors of the QDom classes will only result in empty objects that cannot be manipulated or inserted into the Document.

The QDomDocument class has several functions for creating document data, for example, createElement(), createTextNode(), createComment(), createCDATASection(), createProcessingInstruction(), createAttribute() and createEntityReference(). Some of these functions have versions that support namespaces, i.e. createElementNS() and createAttributeNS(). The createDocumentFragment() function is used to hold parts of the document; this is useful for manipulating for complex documents.

The entire content of the document is set with setContent(). This function parses the string it is passed as an XML document and creates the DOM tree that represents the document. The root element is available using documentElement(). The textual representation of the document can be obtained using toString().

Note: The DOM tree might end up reserving a lot of memory if the XML document is big. For such documents, the QXmlStreamReader or the QXmlQuery classes might be better solutions.

It is possible to insert a node from another document into the document using importNode().

You can obtain a list of all the elements that have a particular tag with elementsByTagName() or with elementsByTagNameNS().
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-5 09:19:56 显示全部楼层
读取和操作XML文件的。。。。。。。。。。。
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-5 09:22:25 显示全部楼层
#include <QCoreApplication>
#include <QtXml>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    // 新建QDomDocument类对象,它代表一个XML文档
    QDomDocument doc;
    QFile file("../myDOM1/my.xml");
    if (!file.open(QIODevice::ReadOnly)) return 0;
    // 将文件内容读到doc中
    if (!doc.setContent(&file)) {
        file.close();
        return 0;
    }
    // 关闭文件
    file.close();
    // 获得doc的第一个结点,即XML说明
    QDomNode firstNode = doc.firstChild();
    // 输出XML说明,nodeName()为“xml”,nodeValue()为版本和编码信息
    qDebug() << qPrintable(firstNode.nodeName())
             << qPrintable(firstNode.nodeValue());

    // 返回根元素
    QDomElement docElem = doc.documentElement();
    // 返回根节点的第一个子结点
    QDomNode n = docElem.firstChild();
    // 如果结点不为空,则转到下一个节点
    while(!n.isNull())
    {
        // 如果结点是元素
        if (n.isElement())
        {
            // 将其转换为元素
            QDomElement e = n.toElement();
            // 返回元素标记和id属性的值
            qDebug() << qPrintable(e.tagName())
                     << qPrintable(e.attribute("id"));
            // 获得元素e的所有子结点的列表
            QDomNodeList list = e.childNodes();
            // 遍历该列表
            for(int i=0; i<list.count(); i++)
            {
                QDomNode node = list.at(i);
                if(node.isElement())
                    qDebug() << "   "<< qPrintable(node.toElement().tagName())
                             <<qPrintable(node.toElement().text());
            }
        }
        // 转到下一个兄弟结点
        n = n.nextSibling();
    }

    return a.exec();
}
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-5 09:24:31 显示全部楼层
《qt creator入门》书上的源代码~~~~~~~~~~~~~~~·
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-5 09:25:10 显示全部楼层
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtXml>
#include <QFile>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    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();
}

MainWindow::~MainWindow()
{
    delete ui;
}

// 显示全部按钮
void MainWindow:n_pushButton_5_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();

    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();
    }
}

// 添加按钮
void MainWindow:n_pushButton_4_clicked()
{
    // 我们先清空显示,然后显示“无法添加!”,这样如果添加失败则会显示“无法添加!”
    ui->listWidget->clear();
    ui->listWidget->addItem(QString("无法添加!"));
    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();
    // 最后更改显示为“添加成功!”
    ui->listWidget->clear();
    ui->listWidget->addItem(QString("添加成功!"));
}

// 对XML文档进行查找、更新和删除操作
void MainWindow::doXml(const QString operate)
{
    ui->listWidget->clear();
    ui->listWidget->addItem(QString("没有找到相关内容!"));
    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(QString("图书"));

    for(int i=0; i<list.count(); i++)
    {
        QDomElement e = list.at(i).toElement();
        if(e.attribute(QString("编号")) == 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(QString("删除成功!"));
            } 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(QString("更新成功!"));
            } else if(operate == "find")  {
                // 如果是查找操作
                ui->listWidget->clear();
                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());
                }
            }
        }
    }
}

// 查找按钮
void MainWindow:n_pushButton_clicked()
{
    doXml("find");
}

// 删除按钮
void MainWindow:n_pushButton_2_clicked()
{
    doXml("delete");
}

// 更新按钮
void MainWindow:n_pushButton_3_clicked()
{
    doXml("update");
}
回复

使用道具 举报

累计签到:43 天
连续签到:1 天
2016-3-5 09:26:10 显示全部楼层
//mainwindow.h文件

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void doXml(const QString operate);

private slots:
    void on_pushButton_5_clicked();

    void on_pushButton_4_clicked();

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
回复

使用道具 举报

累计签到:14 天
连续签到:1 天
2016-3-5 09:28:13 显示全部楼层
奥,我昨天查了关于XML文件的介绍,算是有一些眉目了
回复

使用道具 举报

累计签到:14 天
连续签到:1 天
2016-3-5 11:58:28 显示全部楼层
哈哈,刚才看了你给的说明,又收获了一些东西,谢谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了