当前位置:   article > 正文

《Qt5:读写XML文件QXmlStreamWriter和QXmlStreamReader》_qt qmlstreamwrite写xml文件

qt qmlstreamwrite写xml文件

Qt5读写XML文件需要用到QXmlStreamWriter类和QXmlStreamReader类。

QXmlStreamWriter类

The QXmlStreamWriter class provides an XML writer with a simple streaming API. 

Header:#include <QXmlStreamWriter>
qmake:QT += core
Since:Qt 4.3

QXmlStreamReader类

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帮助文档:

http://doc.qt.io/qt-5/qxmlstreamwriter.html 

http://doc.qt.io/qt-5/qxmlstreamreader.html

 读写XML

打开Qt,新建一个Qt Widgets Application项目,添加如下代码,文件保存路径需要自己稍作修改:

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. namespace Ui {
  5. class MainWindow;
  6. }
  7. class MainWindow : public QMainWindow
  8. {
  9. Q_OBJECT
  10. public:
  11. explicit MainWindow(QWidget *parent = 0);
  12. ~MainWindow();
  13. private:
  14. Ui::MainWindow *ui;
  15. };
  16. #endif // MAINWINDOW_H

mainwindow.cpp

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. //添加的头文件
  4. #include "QXmlStreamWriter"//用于写入XML
  5. #include "QXmlStreamReader"//用于读取XML
  6. #include "QFile"//文件
  7. #include "QDebug"//用于DEBUG输出
  8. MainWindow::MainWindow(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::MainWindow)
  11. {
  12. ui->setupUi(this);
  13. QFile file("C:\\Users\\dell\\Desktop\\Hello.xml");
  14. if (!file.open(QFile::ReadWrite|QIODevice::Truncate))
  15. {
  16. qDebug() << "Error: cannot open file";
  17. }
  18. QXmlStreamWriter stream(&file);
  19. stream.setAutoFormatting(true);
  20. stream.writeStartDocument();
  21. stream.writeStartElement("Information");
  22. stream.writeTextElement("Name", "可乐");
  23. stream.writeTextElement("Kind", "哈士奇");
  24. stream.writeTextElement("Sex", "女");
  25. stream.writeTextElement("BirthDay", "2018/10/22");
  26. stream.writeTextElement("Place","成都");
  27. stream.writeTextElement("Signature","我是一只快乐的小二哈,就是不拆家!");
  28. stream.writeStartElement("Other");
  29. stream.writeTextElement("Address","华润国际社区");
  30. stream.writeEndElement();
  31. stream.writeStartElement("Course");
  32. stream.writeTextElement("Chinese", "中文");
  33. stream.writeTextElement("English", "英文");
  34. stream.writeTextElement("Math", "数学");
  35. stream.writeTextElement("Music", "音乐");
  36. stream.writeEndElement();
  37. stream.writeEndElement();
  38. stream.writeEndDocument();
  39. file.close();
  40. qDebug() << "write finished!";
  41. if(file.open(QIODevice::ReadOnly | QIODevice::Text))
  42. {
  43. //构建QXmlStreamReader对象
  44. QXmlStreamReader reader(&file);
  45. while(!reader.atEnd())
  46. {
  47. //reader.readNext();//读取下一个开始元素
  48. reader.readNextStartElement();//读取下一个开始元素
  49. //判断是否是节点的开始
  50. if(reader.isStartElement())
  51. {
  52. QString strElementName = reader.name().toString();
  53. if (QString::compare(strElementName, "Information") == 0)
  54. {
  55. qDebug() << QString::fromLocal8Bit("**********<Information>********** ");
  56. }
  57. else if (QString::compare(strElementName, QStringLiteral("Name")) == 0)
  58. {
  59. qDebug() << QStringLiteral("Name:%1").arg(reader.readElementText());
  60. }
  61. else if (QString::compare(strElementName, QStringLiteral("Kind")) == 0)
  62. { // 方式
  63. qDebug() << QStringLiteral("Kind:%1").arg(reader.readElementText());
  64. }
  65. else if (QString::compare(strElementName, QStringLiteral("Sex")) == 0)
  66. {
  67. qDebug() << QStringLiteral("Sex:%1").arg(reader.readElementText());
  68. }
  69. else if (QString::compare(strElementName, QStringLiteral("BirthDay")) == 0)
  70. {
  71. qDebug() << QStringLiteral("BirthDay:%1").arg(reader.readElementText());
  72. }
  73. else if (QString::compare(strElementName, QStringLiteral("Place")) == 0)
  74. {
  75. qDebug() << QStringLiteral("Place:%1").arg(reader.readElementText());
  76. }
  77. else if(QString::compare(strElementName, QStringLiteral("Signature")) == 0)
  78. {
  79. qDebug() << QStringLiteral("Signature:%1").arg(reader.readElementText());
  80. }
  81. else if (QString::compare(strElementName, "Other") == 0)
  82. {
  83. qDebug() << QString::fromLocal8Bit("**********<Other>********** ");
  84. QString strName = reader.name().toString();//获取名字
  85. qDebug() << QString("%1").arg(strName);
  86. reader.readNext();//读取下一个开始元素
  87. }
  88. else if(QString::compare(strElementName, QStringLiteral("Address")) == 0)
  89. {
  90. qDebug() << QStringLiteral("Address:%1").arg(reader.readElementText());
  91. reader.readNextStartElement();//读取下一个开始元素
  92. }
  93. else if (QString::compare(strElementName, "Course") == 0)
  94. {
  95. qDebug() << QString::fromLocal8Bit("**********<Course>********** ");
  96. QString strName = reader.name().toString();//获取名字
  97. qDebug() << QString("%1").arg(strName);
  98. }
  99. //reader.readNext();
  100. else if (QString::compare(strElementName, QStringLiteral("Chinese")) == 0)
  101. { // 方式一
  102. //判断是否是字符串
  103. qDebug() << QStringLiteral("Chinese:%1").arg(reader.readElementText());
  104. }
  105. else if (QString::compare(strElementName, QStringLiteral("English")) == 0)
  106. { // 方式
  107. qDebug() << QStringLiteral("English:%1").arg(reader.readElementText());
  108. }
  109. else if (QString::compare(strElementName, QStringLiteral("Sex")) == 0)
  110. {
  111. qDebug() << QStringLiteral("Sex:%1").arg(reader.readElementText());
  112. }
  113. else if (QString::compare(strElementName, QStringLiteral("Math")) == 0)
  114. {
  115. qDebug() << QStringLiteral("Math:%1").arg(reader.readElementText());
  116. }
  117. else if(QString::compare(strElementName, QStringLiteral("Music")) == 0)
  118. {
  119. qDebug() << QStringLiteral("Music:%1").arg(reader.readElementText());
  120. }
  121. }
  122. }
  123. file.close();
  124. }
  125. else
  126. {
  127. qDebug()<<"Open file hello.xml failure";
  128. }
  129. }
  130. MainWindow::~MainWindow()
  131. {
  132. delete ui;
  133. }

写入效果

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Information>
  3.     <Name>可乐</Name>
  4.     <Kind>哈士奇</Kind>
  5.     <Sex>女</Sex>
  6.     <BirthDay>2018/10/22</BirthDay>
  7.     <Place>成都</Place>
  8.     <Signature>我是一只快乐的小二哈,就是不拆家!</Signature>
  9.     <Other>
  10.         <Address>华润国际社区</Address>
  11.     </Other>
  12.     <Course>
  13.         <Chinese>中文</Chinese>
  14.         <English>英文</English>
  15.         <Math>数学</Math>
  16.         <Music>音乐</Music>
  17.     </Course>
  18. </Information>

读取效果

  1. write finished!
  2. "**********<Information>********** "
  3. "Name:可乐"
  4. "Kind:哈士奇"
  5. "Sex:女"
  6. "BirthDay:2018/10/22"
  7. "Place:成都"
  8. "Signature:我是一只快乐的小二哈,就是不拆家!"
  9. "**********<Other>********** "
  10. "Other"
  11. "Address:华润国际社区"
  12. "**********<Course>********** "
  13. "Course"
  14. "Chinese:中文"
  15. "English:英文"
  16. "Math:数学"
  17. "Music:音乐"

完整代码

百度网盘链接:https://pan.baidu.com/s/1hTCkyELF60HStMJq_1Y0zQ 
提取码:uv82 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/781900
推荐阅读
相关标签
  

闽ICP备14008679号