qt xml文件写入读取

******************************************************************************

QT += core gui xml

******************************************************************************

#include "mainwindow.h"

#include <QDomDocument>

#include <QTextStream>

#include <QFile>

#include <QDebug>

MainWindow::MainWindow(QWidget *parent)

: QMainWindow(parent)

{

// 创建文档

QDomDocument docWrite;

// 创建根节点

QDomElement rootWrite = docWrite.createElement("Student");

// 添加根节点到文档中

docWrite.appendChild(rootWrite);

// 创建子节点并设置属性和文本内容

QDomElement name = docWrite.createElement("Name");

QDomText text = docWrite.createTextNode("zhangsan");

name.appendChild(text);

// 将子节点添加到根节点中

rootWrite.appendChild(name);

QDomElement age = docWrite.createElement("Age");

text = docWrite.createTextNode("12");

age.appendChild(text);

rootWrite.appendChild(age);

QDomElement course = docWrite.createElement("Course");

rootWrite.appendChild(course);

QDomElement math = docWrite.createElement("Math");

text = docWrite.createTextNode("100");

math.appendChild(text);

course.appendChild(math);

QDomElement chinese = docWrite.createElement("Chinese");

text = docWrite.createTextNode("90");

chinese.appendChild(text);

course.appendChild(chinese);

// 保存文档到文件

QFile fileWrite("test.xml");

if(fileWrite.open(QIODevice::WriteOnly | QIODevice::Text))

{

QTextStream stream(&fileWrite);

stream << docWrite.toString(4);

fileWrite.close();

}

//

QDomDocument docRead;

QFile fileRead("test.xml");

QString error = "";

int row = 0, column = 0;

if (fileRead.open(QIODevice::ReadOnly))

{

if(!docRead.setContent(&fileRead, false, &error, &row, &column))

{

qDebug() << "parse fileRead failed:" << row << "---" << column <<":" <<error;

fileRead.close();

}

else

{

fileRead.close();

QDomElement rootRead = docRead.documentElement();

QDomNode nodeRead = rootRead.firstChild();

while(!nodeRead.isNull())

{

QDomElement element = nodeRead.toElement();

if(!element.isNull())

{

qDebug() << element.tagName() << ":" << element.text();

QDomNode nodeReadSon = element.firstChild();

while(!nodeReadSon.isNull())

{

QDomElement elementSon = nodeReadSon.toElement();

if(!elementSon.isNull())

{

qDebug() << "---" << elementSon.tagName() << ":" << elementSon.text();

}

nodeReadSon = nodeReadSon.nextSibling();

}

}

nodeRead = nodeRead.nextSibling();

}

}

}

}

MainWindow::~MainWindow()

{

}

******************************************************************************

<Student>

<Name>zhangsan</Name>

<Age>12</Age>

<Course>

<Math>100</Math>

<Chinese>90</Chinese>

</Course>

</Student>

"Name" : "zhangsan"

"Age" : "12"

"Course" : "10090"

--- "Math" : "100"

--- "Chinese" : "90"

相关推荐
Truelon1 小时前
【QT】QT编译链接 msql 数据库
数据库·qt
864记忆10 小时前
车辆视频检测器linux版对于密码中包含敏感字符的处理方法
linux·qt
努力搬砖的咸鱼10 小时前
QTSql全解析:从连接到查询的数据库集成指南
数据库·qt
EverestVIP10 小时前
Qt 自带的QSqlDatabase 模块中使用的 SQLite 和 SQLite 官方提供的 C 语言版本(sqlite.org)对比
c语言·qt·sqlite
永不停转11 小时前
C++宏定义中可变参数列表__VA_ARGS__ 及 QT 提供的宏 QT_OVERLOADED_MACRO
c++·qt
追烽少年x11 小时前
Qt中自定义插件和库(1)
qt
我真的不会C11 小时前
Qt中的信号与槽及其自定义
开发语言·qt
蜡笔弄丢了小新14 小时前
E: The package APP needs to be reinstalled, but I can‘t find an archive for it.
qt
byxdaz15 小时前
Qt 中 findChild和findChildren绑定自定义控件
qt
liulun21 小时前
Windows注册鼠标钩子,获取用户选中的文本
c++·windows·qt