【XML文件的增、删、改、查】

XML文件的增、删、改、查

void MainWindow::onBtnAddClicked()
{
    QString filePath = QCoreApplication::applicationDirPath() + "/hh.xml";
    QFile file(filePath);
    if(!file.open(QFile::ReadOnly)){
        return;
    }

    QDomDocument doc ;
    if(!doc.setContent(&file)){
        file.close();
        return;
    }

    file.close();
    QDomElement root = doc.documentElement();
    QDomElement book = doc.createElement(QString("book"));
    book.setAttribute(QString("id"),3);

    QDomElement author = doc.createElement(QString("author"));
    QDomText text = doc.createTextNode(QStringLiteral("张三"));
    author.appendChild(text);
    book.appendChild(author);

    QDomElement name = doc.createElement(QString("bookName"));
    text = doc.createTextNode(QStringLiteral("《活着》"));
    name.appendChild(text);
    book.appendChild(name);

    QDomElement price = doc.createElement(QString("price"));
    price.setAttribute(QString("money"),35.6);
    book.appendChild(price);

    QDomElement num = doc.createElement(QString("count"));
    text = doc.createTextNode(QString("666"));
    num.appendChild(text);
    book.appendChild(num);
    root.appendChild(book);

    if(!file.open(QFile::WriteOnly|QFile::Truncate)){
        return;
    }
    QTextStream stream(&file);
    doc.save(stream,4);
    file.close();

}

void MainWindow::onBtnDeteteClicked()
{
    QString filePath = QCoreApplication::applicationDirPath() + "/hh.xml";
    QFile file(filePath);

     qDebug()<< filePath;
    if(!file.open(QFile::ReadOnly)){
        return;
    }

    QDomDocument doc;
    if(!doc.setContent(&file)){
            file.close();
            return;
    }

    file.close();

    QDomElement root = doc.documentElement();
    QDomNodeList list = root.elementsByTagName(QString("book"));
    for(int i=0; i<list.size(); i++){
        QDomElement element = list.at(i).toElement();
        if(element.attribute(QString("id")) == QString("1")){
            root.removeChild(list.at(i));
        }
    }

    if(!file.open(QFile::WriteOnly| QFile::Truncate)){
        return;
    }

    QTextStream stream(&file);
    doc.save(stream,4);
    file.close();
    return ;
}

void MainWindow::onBtnUpdateClicked()
{
    QString filePath = QCoreApplication::applicationDirPath() + "/hh.xml";
    QFile file(filePath);
    if(!file.open(QFile::ReadOnly)){
        return;
    }
    QDomDocument doc;
    if(!doc.setContent(&file)){
        file.close();
        return;
    }
    file.close();

    QDomElement root = doc.documentElement();//获取根节点
    QDomNodeList list = root.elementsByTagName(QString("book"));
    QDomNode node = list.at(list.size()-1).firstChild();
    QDomNode oldNode = node.firstChild();
    node.firstChild().setNodeValue(QStringLiteral("曹雪芹"));
    QDomNode newNode = node.firstChild();
    node.replaceChild(newNode,oldNode);
    QDomNode nextNode = node.nextSibling();
    nextNode.firstChild().setNodeValue(QStringLiteral("《红楼梦》"));
    nextNode = nextNode.nextSibling();
    nextNode = nextNode.nextSibling();
    nextNode.firstChild().setNodeValue(QString("900"));

    if(!file.open(QFile::WriteOnly|QFile::Truncate)){
        return;
    }

    QTextStream stream(&file);
    doc.save(stream,4);
    file.close();
    return ;
}

void MainWindow::onBtnReadClicked()
{
    QString filePath = QCoreApplication::applicationDirPath();//获取应用发程序当前路径
    filePath.append(tr("/hh.xml"));

    QFile file(filePath);

    if(!file.open(QFile::ReadOnly)){
        return;
    }

    QDomDocument doc;
    if(!doc.setContent(&file)){//解析xml文件
        file.close();
        return;
    }

    file.close();
    ui->tableWidget->setColumnCount(5);
    ui->tableWidget->setHorizontalHeaderLabels(header);
    int row = 0;
    QDomElement root = doc.documentElement();//获取根节点
    QDomNode node = root.firstChild();//获取子节点
    while (!node.isNull()) {//如果节点不为空
        if(node.isElement()){//如果节点是元素
             ui->tableWidget->setRowCount(row+1);
            QDomElement element = node.toElement();//转换为元素,注意元素和节点是2个数据结构
            QDomNodeList list = element.childNodes();
            qDebug()<< element.tagName() << element.attribute("id");
            ui->tableWidget->setItem(row,0,new QTableWidgetItem(element.attribute("id")));
            for(int i=0; i<list.size();i++){
                QDomNode n = list.at(i);
                if(node.isElement()){
                    qDebug() << n.nodeName() << ": " << n.toElement().text();
                    ui->tableWidget->setItem(row,i+1,new QTableWidgetItem(n.toElement().text()));
                }
            }
            row++;
        }
        node = node.nextSibling();//指向下一个兄弟节点
    }

}

创建xml文件

void MainWindow::onBtnCreateClicked()
{
    QString filePath = QCoreApplication::applicationDirPath();//获取应用发程序当前路径
    filePath.append(tr("/hh.xml"));
    QFile file(filePath);
    if(!file.open(QFile::WriteOnly|QFile::Truncate)){//Truncate:清空原文件数据
        return;
    }

    ui->tableWidget->clear();
    ui->tableWidget->setRowCount(0);
    ui->tableWidget->setHorizontalHeaderLabels(header);


    //1、添加头部
    QDomDocument doc;//"version=\"1.0\" encoding=\"UTF-8\""
    QDomProcessingInstruction instruct = doc.createProcessingInstruction(QString("xml"),QString("version =\"1.0\" encoding=\"UTF-8\""));
    doc.appendChild(instruct);

    //2、添加根节点
    QDomElement root = doc.createElement(QString("library"));
    doc.appendChild(root);

    //3、添加子节点
    QDomElement book = doc.createElement(QString("book"));
    book.setAttribute(QString("id"),1);

    QDomElement author = doc.createElement(QString("author"));
    QDomText text = doc.createTextNode(QString("LHG"));
    author.appendChild(text);
    book.appendChild(author);

    QDomElement name = doc.createElement(QString("bookName"));
    text = doc.createTextNode(QString("C++"));
    name.appendChild(text);
    book.appendChild(name);

    QDomElement price = doc.createElement(QString("price"));
    price.setAttribute(QString("money"),33.6);
    book.appendChild(price);

    QDomElement num = doc.createElement(QString("count"));
    text = doc.createTextNode(QString("100"));
    num.appendChild(text);
    book.appendChild(num);
    root.appendChild(book);

    //添加第二个节点
    book = doc.createElement(QString("book"));
    book.setAttribute(QString("id"),2);

    author = doc.createElement(QString("author"));
    text = doc.createTextNode(QString("GuiGe"));
    author.appendChild(text);
    book.appendChild(author);

    name = doc.createElement(QString("bookName"));
    text = doc.createTextNode(QStringLiteral("三国演义"));
    name.appendChild(text);
    book.appendChild(name);

    price = doc.createElement(QString("price"));
    price.setAttribute(QString("money"),156.6);
    book.appendChild(price);

    num = doc.createElement(QString("count"));


    text = doc.createTextNode(QString("200"));
    num.appendChild(text);
    book.appendChild(num);
    root.appendChild(book);

    QTextStream stream(&file);
    doc.save(stream,4);
    file.close();
}

设置按钮按下不同颜色

 ui->pushButton-> setStyleSheet("QPushButton{background-color:rgb(19, 100, 181);color: white; } QPushButton:pressed{background-color:rgb(134, 134, 134)}");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_44585751

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值