之前写过的代码,现在又忘了,特在此记录下QT对XML配置文件的操作,基本功能都实现了,下面的代码仅供参考。
- 简介
QT读取、保存xml文件 - 环境:
QT 5.5.1、test.xml、win7 32位 - 语言:C++
1.首先必须在xxx.pro文件中添加一行代码:
QT += core gui xml
- MyXML.h文件
#include <QObject>
#include <QFile>
#include <QMap>
#include <QDomDocument> //涉及到的头文件
struct StationSN
{
QString readySN;
QString runningSN;
QString doneSN;
};
class MyXML
{
public:
MyXML();
/*************************************
* 功能:打开xml文件
*************************************/
bool OpenXML();
/*************************************
* 功能:在元素集中查找是否有某个标签
* 输入:srcElement 源元素集
* key 要查找的标签
* 输出:element 该标签所在的元素集合
* 返回值:若找到则返回true
*************************************/
bool FindElement(QDomElement &srcElement, QDomElement &element,QString key);
/*************************************
* 功能:获取test.xml文件中的信息
*************************************/
void GetStationSN();
/*************************************
* 功能:保存到.xml文件
*************************************/
void Save();
private:
QMap<int, StationSN> stationSNMap;
QDomDocument doc;
QDomElement root;
};
- MyXML.cpp文件:
#include "myxml.h"
#include <QDebug>
MyXML::MyXML()
{
}
bool MyXML::OpenXML()
{
QString fileName = "test.xml";
QFile *file;
if(!file->exists(fileName))
{
qDebug() << fileName << " not exist";
return false;
}
else
{
file = new QFile(fileName);
if(!file->open(QIODevice::ReadOnly | QFile::Text))
{
qDebug() << fileName << " open filed";
return false;
}
QString error;
int row = 0, column = 0;
if(!doc.setContent(file, false, &error, &row, &column))
{
qDebug() << "parse file failed at line row and column" + QString::number(row, 10) + QString(",") + QString::number(column, 10);
file->close();
return false;
}
root = doc.documentElement();
file->close();
}
return true;
}
bool MyXML::FindElement(QDomElement &srcElement, QDomElement &element, QString key)
{
QDomNode node = srcElement.firstChild();
while(!node.isNull())
{
if(node.isElement())
{
QDomElement domElement = node.toElement();
if( domElement.tagName()==key )
{
element = domElement;
return true;
}
}
node = node.nextSibling();
}
return false;
}
void MyXML::GetMsg()
{
QDomElement domElement;
if( !FindElement(root, domElement, "Station") ) //查找"Station"标签
{
qDebug() << "not found Station in test.xml";
return;
}
QDomNodeList list = root.childNodes();
for(int i=0; i<list.count(); i++)
{
QDomElement dom;
dom = list.at(i).toElement();
QDomNodeList nodeList = dom.childNodes();
stationSNMap[i].readySN = nodeList.at(1).toElement().text();
stationSNMap[i].runningSN = nodeList.at(2).toElement().text();
stationSNMap[i].doneSN = nodeList.at(3).toElement().text();
}
}
void SaveData::Save()
{
if(!OpenXML())
{
return;
}
//root是xml中的根,即<SN>标签
QDomNodeList list = root.elementsByTagName("Station");
//此处修改的是<SN>标签下第2个(索引从0开始)<Station>标签下第3个元素的值,修改为"hello"
//修改前:<runningSN>0</runningSN> 修改后:<runningSN>hello</runningSN>
list.at(1).toElement().childNodes().at(2).toElement().firstChild().setNodeValue("hello");
QFile *file = new QFile("test.xml");
if(!file->open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug() << "open for remove error!";
}
QTextStream out(file);
doc.save(out,4);
file->close();
}
4.test.xml文件
<?xml version='1.0' encoding='UTF-8'?>
<SN>
<Station>
<index>0</index>
<readySN>0</readySN>
<runningSN>0</runningSN>
<doneSN>0</doneSN>
</Station>
<Station>
<index>1</index>
<readySN>0</readySN>
<runningSN>hello</runningSN>
<doneSN>0</doneSN>
</Station>
<Station>
<index>2</index>
<readySN>0</readySN>
<runningSN>0</runningSN>
<doneSN>0</doneSN>
</Station>
</SN>