一:需求
通过程序的方式对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;
}
四:结果