Qt的XML文件处理

前端之家收集整理的这篇文章主要介绍了Qt的XML文件处理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上次随便写了些关于Qt5的事件与处理,没想到还会有人看,所以这次就认真写了一篇关于Qt中的XML文件处理的文章,如有知识上的错误还望大家指点~


这篇文章主要是解决以下问题:(如果需要的话可以参考一下)

·XML文件的存放

·XML文件的解析

·XML文件信息的查询



·XML文件的存放

XML文件的存放对知道的人来说简单,但是对没用过Qt的人来说放错地方了那可真是要命,直接会导致从一开始文件就读取错误

XML文件与头文件、源文件不同,而是放在对应的debug文件夹或release文件夹内


·XML文件的解析

文件的解析依然是那些老套路

  1. QFile *file;
  1. file = new QFile("attribs.xml"); //关联attrib.xml文件


void MainWindow::ReadFile() //读取文件

  1. {
  1. if(!file->open(QIODevice::ReadOnly)) //以只读的方式打开文件
  1. {
  1. return;
  1. }
  1. if(!filedata.setContent(file)) //解析XML文件并设置为filedata的内容
  1. { //This function parses the XML document from the byte array data and sets it as the content of the document.
  1. file->close();
  1. return;
  1. }
  1. file->close();
  1. }

setContent()返回解析结果,结果为bool值,解析成功后会返回true,否则返回false

·XML文件信息的查询

查询信息之前先要创建好XML文件,例:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <archive>
  3. <car id="1" >
  4. <attrib number="01" >排量:2393ml</attrib>
  5. <attrib number="02" >价格:43.26万元</attrib>
  6. <attrib number="03" >排放:欧 4</attrib>
  7. <attrib number="04" >油耗:7.01(90km/h) 8.31(120km/h)</attrib>
  8. <attrib number="05" >功率:130/6000</attrib>
  9. </car>
  10. <car id="2" >
  11. <attrib number="01" >排量:1600ml</attrib>
  12. <attrib number="02" >价格:8.26万元</attrib>
  13. <attrib number="03" >排放:欧 3</attrib>
  14. <attrib number="04" >油耗:6.11(90km/h)</attrib>
  15. <attrib number="05" >功率:68/5800</attrib>
  16. </car>
  17. </archive>

  1.  
  1.  
  1. ·搜寻节点
  1. void MainWindow::showDetail()      //搜寻节点
  2. {
  3.     QDomNodeList cars = filedata.elementsByTagName("car");      //查找所有"car"元素节点,返回QDomNodeList实例
  4.     //检测节点数用
  5.     //int textNumber = cars.count();
  6.     QString ID("2");
  7.     for(int i = 0; i < cars.count(); i++)   //遍历节点
  8.     {
  9.         QDomNode car = cars.item(i);
  10.         QString test(car.toElement().attribute("id"));      //toElement()函数将QDomNode转化为QDomElement,attribute("id")查找id属性,返回值为QString
  11.         if(car.toElement().attribute("id") == ID)        //这里的ID是检验用的,来匹配元素节点中id对应为ID的节点(可调整)
  12.         {
  13.             getAttribList(car);     //得到节点后进一步查询信息
  14.             break;
  15.         }
  16.     }
  17. }

  1. ·获取信息
  1.  
  1. void MainWindow::getAttribList(QDomNode &car) //获取信息
  2. {
  3. QDomNodeList nodes = car.childNodes(); //childNodes()返回节点的子节点集合,返回值为QDomNodeList实例
  4. QDomNode node;
  5. QString getNumber;
  6. for(int i = 0; i < nodes.count(); i++) //以QDomNodeList的nodes的子节点数来遍历子节点提取需要的信息
  7. {
  8. node = nodes.item(i);
  9. getNumber = node.toElement().attribute("number"); //获取子节点的number属性对应信息
  10. QString infor(getNumber+" "+node.toElement().text()); //node.toElement().text()对应节点元素文本
  11.  
  12. QMessageBox::about(this,"infor",infor);
  13. }
  14. }
  1.  
  1.  
  1.  
  1. //下面是源码(xml文件用上面的就可以了):
  1.  
  1.  
  1. //mainwindow.h
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QFile>
  6. #include <QDomDocument>
  7. #include <QDomNode>
  8. #include <QMessageBox>
  9. #include <QDomNodeList>
  10.  
  11. class MainWindow : public QMainWindow
  12. {
  13. Q_OBJECT
  14.  
  15. public:
  16. MainWindow(QWidget *parent = 0);
  17.  
  18. QFile *file;
  19.  
  20. QDomDocument filedata;
  21.  
  22. void showDetail();
  23. void ReadFile();
  24. void getAttribList(QDomNode &car);
  25.  
  26. ~MainWindow();
  27. };
  28.  
  29. #endif // MAINWINDOW_H
  1.  
  1.  
  1.  
  1.  
  1. #include "mainwindow.h"
  2.  
  3. MainWindow::MainWindow(QWidget *parent)
  4. : QMainWindow(parent)
  5. {
  6. file = new QFile("attribs.xml"); //关联attrib.xml文件
  7.  
  8. ReadFile();
  9.  
  10. showDetail();
  11.  
  12. }
  13.  
  14. void MainWindow::showDetail() //搜寻节点
  15. {
  16. QDomNodeList cars = filedata.elementsByTagName("car"); //查找所有"car"元素节点,返回QDomNodeList实例
  17.  
  18. //检测节点数用
  19. //int textNumber = cars.count();
  20.  
  21. QString ID("2");
  22.  
  23. for(int i = 0; i < cars.count(); i++) //遍历节点
  24. {
  25. QDomNode car = cars.item(i);
  26. QString test(car.toElement().attribute("id")); //toElement()函数将QDomNode转化为QDomElement,attribute("id")查找id属性,返回值为QString
  27. if(car.toElement().attribute("id") == ID) //这里的ID是检验用的,来匹配元素节点中id对应为ID的节点(可调整)
  28. {
  29. getAttribList(car); //得到节点后进一步查询信息
  30. break;
  31. }
  32. }
  33. }
  34.  
  35. void MainWindow::getAttribList(QDomNode &car) //获取信息
  36. {
  37. QDomNodeList nodes = car.childNodes(); //childNodes()返回节点的子节点集合,返回值为QDomNodeList实例
  38. QDomNode node;
  39. QString getNumber;
  40. for(int i = 0; i < nodes.count(); i++) //以QDomNodeList的nodes的子节点数来遍历子节点提取需要的信息
  41. {
  42. node = nodes.item(i);
  43. getNumber = node.toElement().attribute("number"); //获取子节点的number属性对应信息
  44. QString infor(getNumber+" "+node.toElement().text()); //node.toElement().text()对应节点元素文本
  45.  
  46. QMessageBox::about(this,infor);
  47. }
  48. }
  49.  
  50. void MainWindow::ReadFile()
  51. {
  52. if(!file->open(QIODevice::ReadOnly)) //以只读的方式打开文件
  53. {
  54. return;
  55. }
  56. if(!filedata.setContent(file)) //解析XML文件并设置为filedata的内容
  57. { //This function parses the XML document from the byte array data and sets it as the content of the document.
  58. file->close();
  59. return;
  60. }
  61. file->close();
  62. }
  63.  
  64. MainWindow::~MainWindow()
  65. {
  66.  
  67. }

main.cpp文件未做修改

猜你在找的XML相关文章