赞
踩
Qt5读写XML文件需要用到QXmlStreamWriter类和QXmlStreamReader类。
The QXmlStreamWriter class provides an XML writer with a simple streaming API.
Header: #include <QXmlStreamWriter> qmake: QT += core Since: Qt 4.3
The QXmlStreamReader class provides a fast parser for reading well-formed XML via a simple streaming API.
Header: #include <QXmlStreamReader> qmake: QT += core Since: Qt 4.3
Qt帮助文档:
打开Qt,新建一个Qt Widgets Application项目,添加如下代码,文件保存路径需要自己稍作修改:
mainwindow.h
- #ifndef MAINWINDOW_H
- #define MAINWINDOW_H
-
- #include <QMainWindow>
-
- namespace Ui {
- class MainWindow;
- }
-
- class MainWindow : public QMainWindow
- {
- Q_OBJECT
-
- public:
- explicit MainWindow(QWidget *parent = 0);
- ~MainWindow();
-
- private:
- Ui::MainWindow *ui;
- };
-
- #endif // MAINWINDOW_H
mainwindow.cpp
- #include "mainwindow.h"
- #include "ui_mainwindow.h"
- //添加的头文件
- #include "QXmlStreamWriter"//用于写入XML
- #include "QXmlStreamReader"//用于读取XML
- #include "QFile"//文件
- #include "QDebug"//用于DEBUG输出
-
- MainWindow::MainWindow(QWidget *parent) :
- QMainWindow(parent),
- ui(new Ui::MainWindow)
- {
- ui->setupUi(this);
- QFile file("C:\\Users\\dell\\Desktop\\Hello.xml");
- if (!file.open(QFile::ReadWrite|QIODevice::Truncate))
- {
- qDebug() << "Error: cannot open file";
- }
-
-
- QXmlStreamWriter stream(&file);
- stream.setAutoFormatting(true);
- stream.writeStartDocument();
-
- stream.writeStartElement("Information");
- stream.writeTextElement("Name", "可乐");
- stream.writeTextElement("Kind", "哈士奇");
- stream.writeTextElement("Sex", "女");
- stream.writeTextElement("BirthDay", "2018/10/22");
- stream.writeTextElement("Place","成都");
- stream.writeTextElement("Signature","我是一只快乐的小二哈,就是不拆家!");
- stream.writeStartElement("Other");
- stream.writeTextElement("Address","华润国际社区");
- stream.writeEndElement();
-
-
- stream.writeStartElement("Course");
- stream.writeTextElement("Chinese", "中文");
- stream.writeTextElement("English", "英文");
- stream.writeTextElement("Math", "数学");
- stream.writeTextElement("Music", "音乐");
- stream.writeEndElement();
- stream.writeEndElement();
-
- stream.writeEndDocument();
- file.close();
-
- qDebug() << "write finished!";
- if(file.open(QIODevice::ReadOnly | QIODevice::Text))
- {
-
- //构建QXmlStreamReader对象
- QXmlStreamReader reader(&file);
-
- while(!reader.atEnd())
- {
- //reader.readNext();//读取下一个开始元素
- reader.readNextStartElement();//读取下一个开始元素
- //判断是否是节点的开始
- if(reader.isStartElement())
- {
- QString strElementName = reader.name().toString();
- if (QString::compare(strElementName, "Information") == 0)
- {
- qDebug() << QString::fromLocal8Bit("**********<Information>********** ");
- }
- else if (QString::compare(strElementName, QStringLiteral("Name")) == 0)
- {
- qDebug() << QStringLiteral("Name:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, QStringLiteral("Kind")) == 0)
- { // 方式
- qDebug() << QStringLiteral("Kind:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, QStringLiteral("Sex")) == 0)
- {
- qDebug() << QStringLiteral("Sex:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, QStringLiteral("BirthDay")) == 0)
- {
- qDebug() << QStringLiteral("BirthDay:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, QStringLiteral("Place")) == 0)
- {
- qDebug() << QStringLiteral("Place:%1").arg(reader.readElementText());
- }
- else if(QString::compare(strElementName, QStringLiteral("Signature")) == 0)
- {
- qDebug() << QStringLiteral("Signature:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, "Other") == 0)
- {
- qDebug() << QString::fromLocal8Bit("**********<Other>********** ");
- QString strName = reader.name().toString();//获取名字
- qDebug() << QString("%1").arg(strName);
- reader.readNext();//读取下一个开始元素
- }
- else if(QString::compare(strElementName, QStringLiteral("Address")) == 0)
- {
- qDebug() << QStringLiteral("Address:%1").arg(reader.readElementText());
- reader.readNextStartElement();//读取下一个开始元素
- }
- else if (QString::compare(strElementName, "Course") == 0)
- {
- qDebug() << QString::fromLocal8Bit("**********<Course>********** ");
- QString strName = reader.name().toString();//获取名字
- qDebug() << QString("%1").arg(strName);
- }
- //reader.readNext();
- else if (QString::compare(strElementName, QStringLiteral("Chinese")) == 0)
- { // 方式一
- //判断是否是字符串
- qDebug() << QStringLiteral("Chinese:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, QStringLiteral("English")) == 0)
- { // 方式
- qDebug() << QStringLiteral("English:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, QStringLiteral("Sex")) == 0)
- {
- qDebug() << QStringLiteral("Sex:%1").arg(reader.readElementText());
- }
- else if (QString::compare(strElementName, QStringLiteral("Math")) == 0)
- {
- qDebug() << QStringLiteral("Math:%1").arg(reader.readElementText());
- }
- else if(QString::compare(strElementName, QStringLiteral("Music")) == 0)
- {
- qDebug() << QStringLiteral("Music:%1").arg(reader.readElementText());
- }
- }
-
- }
- file.close();
- }
- else
- {
- qDebug()<<"Open file hello.xml failure";
- }
- }
-
- MainWindow::~MainWindow()
- {
- delete ui;
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <Information>
- <Name>可乐</Name>
- <Kind>哈士奇</Kind>
- <Sex>女</Sex>
- <BirthDay>2018/10/22</BirthDay>
- <Place>成都</Place>
- <Signature>我是一只快乐的小二哈,就是不拆家!</Signature>
- <Other>
- <Address>华润国际社区</Address>
- </Other>
- <Course>
- <Chinese>中文</Chinese>
- <English>英文</English>
- <Math>数学</Math>
- <Music>音乐</Music>
- </Course>
- </Information>
- write finished!
- "**********<Information>********** "
- "Name:可乐"
- "Kind:哈士奇"
- "Sex:女"
- "BirthDay:2018/10/22"
- "Place:成都"
- "Signature:我是一只快乐的小二哈,就是不拆家!"
- "**********<Other>********** "
- "Other"
- "Address:华润国际社区"
- "**********<Course>********** "
- "Course"
- "Chinese:中文"
- "English:英文"
- "Math:数学"
- "Music:音乐"
百度网盘链接:https://pan.baidu.com/s/1hTCkyELF60HStMJq_1Y0zQ
提取码:uv82
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。