QT通过Dom树修改XML文件

一:需求
通过程序的方式对xml文件进行修改,比如自己做一个xml文件修改器,只需要向文本框里面输入你要修改的内容,然后点击编辑,就可以修改相应的xml文件了。

二:方法
QT提供了多种方法去对xml文件进行读改操作,例如DOM方式和SAX方式,这两种方法适用于不同的情况,看你自己xml文件的大小然后决定用哪种方式,我这里是用的DOM树的方式。

三:实现
1,test.xml

<?xml version="1.0" encoding="UTF-8"?>
<rootName>
    <node1Name1>
        <node2Name1>abc</node2Name1>
        <node2Name2>hahahaha</node2Name2>
    </node1Name1>
</rootName>

2,要使用DOM树需要在pro文件里添加相应库
在这里插入图片描述
3,mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QDomElement>         //新增

namespace Ui {
class MainWindow;
}


class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

    bool openXmlFile(QString FilePath);
    bool changeXml();

private:
    Ui::MainWindow *ui;

    QDomDocument m_doc;        //新增
};

#endif // MAINWINDOW_H

4,mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <QDebug>

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

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


bool MainWindow::openXmlFile(QString FilePath)
{
    QFile file( FilePath );
    if( !file.open( QFile::ReadOnly | QFile::Text  ) )
    {
        qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->file.open->%s\n") << FilePath;

        return false;
    }

    if( !m_doc.setContent( &file ) )
    {
        qDebug() << QObject::tr("error::ParserXML->OpenXmlFile->doc.setContent\n") << FilePath;

        file.close();
        return false;
    }

    file.close();
    return true;
}

bool MainWindow::changeXml()
{
    QString rootName = "rootName";         //根结点
    QString node1Name1 = "node1Name1";     //第1层第1个子结点
    QString node2Name1 = "node2Name1";     //第2层第1个子结点
    QString node2Name2 = "node2Name2";     //第2层第2个子结点

    //获取文件内容
    if(!openXmlFile(qApp->applicationDirPath()+"/test_xml/test.xml"))
    {
        return false;
    }

    //获取根结点元素
    QDomElement root = m_doc.documentElement();
    if(root.tagName().compare(rootName) != 0)
    {
        qDebug() << "rootName does not match";
        return false;
    }

    //获取子节点
    QDomNode node = root.firstChild();
    while ( !node.isNull() )
    {
        //获取第1层子结点元素
        QDomElement nodeElement = node.toElement();
        if(!nodeElement.isNull())
        {
            //判断第1层子节点的名字
            if( nodeElement.nodeName().compare(node1Name1) == 0)
            {
                //获取第1层子节点列表, 并遍历
                QDomNodeList list = nodeElement.childNodes();
                for(int i=0; i<list.count(); i++)
                {
                    //获取第2层第1个/2个子节点
                    QDomNode node = list.at(i);
                    if(node.isElement())
                    {
                        if( node.nodeName().compare(node2Name1) == 0)
                        {
                            //获取第3层第1个字结点,并修改值
                            QDomNode oldnode = node.firstChild();
                            node.firstChild().setNodeValue("csdn");
                            QDomNode newnode = node.firstChild();
                            node.replaceChild(newnode,oldnode);
                        }
                        if( node.nodeName().compare(node2Name2) == 0)
                        {
                           QDomNode oldnode = node.firstChild();
                           node.firstChild().setNodeValue("www.stu.edu.cn");
                           QDomNode newnode = node.firstChild();
                           node.replaceChild(newnode,oldnode);
                        }
                   }
                }
            }
        }

        //第1层子结点的下一结点
        node = node.nextSibling();
    }

    //重写入文件
    QFile filexml(qApp->applicationDirPath()+"/test_xml/test.xml");
    if( !filexml.open( QFile::WriteOnly | QFile::Truncate) )
    {
        qWarning("error::ParserXML->writeOperateXml->file.open\n");
        return false;
    }

    QTextStream ts(&filexml);
    ts.reset();
    ts.setCodec("utf-8");
    m_doc.save(ts, 4, QDomNode::EncodingFromTextStream);
    filexml.close();

    return true;
}

四:结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

路漫漫其远,吾求索

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

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

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

打赏作者

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

抵扣说明:

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

余额充值