QT中使用QXmlStreamReader解析XML文件

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

转载请注明链接与作者huihui1988

XML文件由于其扩展性与兼容性的优点,被广泛用作软件和系统的配置文件。这里简要介绍一下QT下如何来解析XML文件

代码

xml_reader.h

  1. #ifndefXML_READER_H
  2. #defineXML_READER_H
  3. #include<QtCore>
  4. //#include<QtGui>
  5. classxml_reader:publicQXmlStreamReader
  6. {
  7. //Q_OBJECT
  8. public:
  9. xml_reader();
  10. boolread(QIODevice*device);
  11. private:
  12. voidread_data();
  13. voidread_startweek();
  14. voidread_eventnumber();
  15. voidread_period();
  16. voidread_id();
  17. voidread_starttime();
  18. voidread_endtime();
  19. voidread_exception();
  20. voidread_subexception();
  21. voidread_description();
  22. voidread_duration();
  23. voidread_event();
  24. voidread_offsettime();
  25. voidread_eventdesc();
  26. voidread_eventname();
  27. voidread_prompt();
  28. voidread_lasthandle();
  29. voidread_unknow();
  30. };
  31. #endif//XML_READER_H

xml_reader.cpp

copy
    #include"xml_reader.h"
  1. xml_reader::xml_reader()
  2. {}
  3. boolxml_reader::read(QIODevice*device)
  4. {
  5. setDevice(device);
  6. while(!atEnd())
  7. readNext();
  8. if(isStartElement())
  9. if(name()=="data")
  10. read_data();
  11. }
  12. }
  13. return!error();
  14. voidxml_reader::read_data()
  15. qDebug()<<"Starttoreaddata...";
  16. if(name()=="period")
  17. read_period();
  18. if(name()=="startweek")
  19. read_startweek();
  20. if(name()=="eventnumber")
  21. read_eventnumber();
  22. if(isEndElement())
  23. if(name()=="data")
  24. qDebug()<<"Nowattheendofdataelement...";
  25. break;
  26. voidxml_reader::read_startweek()
  27. qDebug()<<"startweekis:"<<readElementText();
  28. voidxml_reader::read_eventnumber()
  29. qDebug()<<"eventNumberis:"<<readElementText();
  30. voidxml_reader::read_period()
  31. qDebug()<<"Starttoreadperiod...";
  32. while(!atEnd())
  33. readNext();
  34. if(isStartElement())
  35. if(name()=="id")
  36. read_id();
  37. if(name()=="starttime")
  38. read_starttime();
  39. if(name()=="endtime")
  40. read_endtime();
  41. if(name()=="exception")
  42. read_exception();
  43. if(name()=="duration")
  44. read_duration();
  45. if(name()=="event")
  46. read_event();
  47. if(isEndElement())
  48. qDebug()<<"Nowattheendofperiod...";
  49. break;
  50. voidxml_reader::read_id()
  51. qDebug()<<"idis:"<<readElementText();
  52. voidxml_reader::read_starttime()
  53. qDebug()<<"starttimeis:"<<readElementText();
  54. voidxml_reader::read_endtime()
  55. qDebug()<<"endtimeis:"<<readElementText();
  56. voidxml_reader::read_exception()
  57. qDebug()<<"Starttoreadexception...";
  58. if(name()=="subexception")
  59. read_subexception();
  60. qDebug()<<"Nowattheendofexception...";
  61. voidxml_reader::read_subexception()
  62. qDebug()<<"Starttoreadsubexception...";
  63. if(name()=="description")
  64. read_description();
  65. qDebug()<<"Nowattheendofsubexception...";
  66. voidxml_reader::read_description()
  67. qDebug()<<"descriptionis:"<<readElementText();
  68. voidxml_reader::read_duration()
  69. qDebug()<<"durationis:"<<readElementText();
  70. voidxml_reader::read_event()
  71. qDebug()<<"Starttoreadevent...";
  72. if(name()=="id")
  73. read_id();
  74. if(name()=="offsettime")
  75. read_offsettime();
  76. if(name()=="eventdesc")
  77. read_eventdesc();
  78. if(name()=="lasthandle")
  79. read_lasthandle();
  80. qDebug()<<"Nowattheendofevent..";
  81. voidxml_reader::read_offsettime()
  82. qDebug()<<"offsettimeis:"<<readElementText();
  83. voidxml_reader::read_eventdesc()
  84. qDebug()<<"Starttoreadeventdesc...";
  85. if(name()=="eventname")
  86. read_eventname();
  87. if(name()=="prompt")
  88. read_prompt();
  89. qDebug()<<"Nowattheendofeventdesc...";
  90. voidxml_reader::read_eventname()
  91. qDebug()<<"eventnameis:"<<readElementText();
  92. voidxml_reader::read_prompt()
  93. qDebug()<<"promptis:"<<readElementText();
  94. voidxml_reader::read_lasthandle()
  95. qDebug()<<"lasthandleis:"<<readElementText();
  96. }
main.cpp
copy
    #include<QtCore/QCoreApplication>
  1. #include"XmlPraser.h"
  2. intmain(intargc,char*argv[])
  3. QCoreApplicationa(argc,argv);
  4. QFilefile("praser.xml");
  5. XmlPraserreader;
  6. if(file.open(QIODevice::ReadOnly|QIODevice::Text))
  7. qDebug()<<"Filehasloaded...";
  8. reader.read(&file);
  9. else
  10. qDebug()<<"Nofiletoread...";
  11. file.close();
  12. qDebug()<<"Pressctrl+Ctoexit...";
  13. returna.exec();
  14. }

XML文件范例praser.xml

[xhtml] copy
    <?xmlversion="1.0"encoding="GBK"?>
  1. <data>
  2. startweek>2010-03-0100:00:00.000</>
  3. eventnumber>2periodid>223401starttimeendtime>2010-06-2023:59:59.999exceptionsubexception>01>2010-04-0300:00:00.000>2010-04-0523:59:59.000description>清明节放假三天>02>2010-05-0100:00:00.000>2010-05-0323:59:59.000>五一劳动节放假三天duration>0000-00-0700:00:00.000eventoffsettime>0000-00-0014:00:00.000eventdesceventname>编译原理prompt>ringlasthandle>null>0000-00-0309:50:00.000>vibration>

由于是使用的QXmlStrramReader来进行解析,因而所有的节点都需要自己编写函数进行相应的处理。

分享到:

猜你在找的XML相关文章