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"

相关推荐
feiyangqingyun2 小时前
Qt音视频开发技巧/推流带旋转角度/rtsprtmp推流/保存文件到MP4/拉流解析旋转角度
qt·音视频·qt旋转角度推流
清醒的兰3 小时前
Qt 基于TCP套接字编程
网络·qt·tcp
mahuifa10 天前
PySide环境配置及工具使用
python·qt·环境配置·开发经验·pyside
小灰灰搞电子10 天前
Qt PyQt与PySide技术-C++库的Python绑定
c++·qt·pyqt
Echo``10 天前
12.OpenCV—基础入门
开发语言·c++·人工智能·qt·opencv·计算机视觉
智驾10 天前
QML革命:下一代GUI开发的核心优势详解
qt·qml
五_谷_丰_登10 天前
mongoDB服务本地化部署
数据库·c++·qt·mongodb
tangchao340勤奋的老年?10 天前
Qt QMap数据清除测试(验证QMap内存正确释放方法)
开发语言·qt
攻城狮7号10 天前
【AI时代速通QT】第二节:Qt SDK 的目录介绍和第一个Qt Creator项目
c语言·c++·qt·跨平台
hvinsion10 天前
【开源工具】一键解决使用代理后无法访问浏览器网页问题 - 基于PyQt5的智能代理开关工具开发全攻略
开发语言·python·qt·开源·vpn·代理服务器