转载请注明链接与作者huihui1988
XML文件由于其扩展性与兼容性的优点,被广泛用作软件和系统的配置文件。这里简要介绍一下QT下如何来解析XML文件。
源代码:
xml_reader.h
- #ifndefXML_READER_H
- #defineXML_READER_H
- #include<QtCore>
- //#include<QtGui>
- classxml_reader:publicQXmlStreamReader
- {
- //Q_OBJECT
- public:
- xml_reader();
- boolread(QIODevice*device);
- private:
- voidread_data();
- voidread_startweek();
- voidread_eventnumber();
- voidread_period();
- voidread_id();
- voidread_starttime();
- voidread_endtime();
- voidread_exception();
- voidread_subexception();
- voidread_description();
- voidread_duration();
- voidread_event();
- voidread_offsettime();
- voidread_eventdesc();
- voidread_eventname();
- voidread_prompt();
- voidread_lasthandle();
- voidread_unknow();
- };
- #endif//XML_READER_H
xml_reader.cpp
copy
- #include"xml_reader.h"
- xml_reader::xml_reader()
- {}
- boolxml_reader::read(QIODevice*device)
- {
- setDevice(device);
- while(!atEnd())
- readNext();
- if(isStartElement())
- if(name()=="data")
- read_data();
- }
- }
- return!error();
- voidxml_reader::read_data()
- qDebug()<<"Starttoreaddata...";
- if(name()=="period")
- read_period();
- if(name()=="startweek")
- read_startweek();
- if(name()=="eventnumber")
- read_eventnumber();
- if(isEndElement())
- if(name()=="data")
- qDebug()<<"Nowattheendofdataelement...";
- break;
- voidxml_reader::read_startweek()
- qDebug()<<"startweekis:"<<readElementText();
- voidxml_reader::read_eventnumber()
- qDebug()<<"eventNumberis:"<<readElementText();
- voidxml_reader::read_period()
- qDebug()<<"Starttoreadperiod...";
- while(!atEnd())
- readNext();
- if(isStartElement())
- if(name()=="id")
- read_id();
- if(name()=="starttime")
- read_starttime();
- if(name()=="endtime")
- read_endtime();
- if(name()=="exception")
- read_exception();
- if(name()=="duration")
- read_duration();
- if(name()=="event")
- read_event();
- if(isEndElement())
- qDebug()<<"Nowattheendofperiod...";
- break;
- voidxml_reader::read_id()
- qDebug()<<"idis:"<<readElementText();
- voidxml_reader::read_starttime()
- qDebug()<<"starttimeis:"<<readElementText();
- voidxml_reader::read_endtime()
- qDebug()<<"endtimeis:"<<readElementText();
- voidxml_reader::read_exception()
- qDebug()<<"Starttoreadexception...";
- if(name()=="subexception")
- read_subexception();
- qDebug()<<"Nowattheendofexception...";
- voidxml_reader::read_subexception()
- qDebug()<<"Starttoreadsubexception...";
- if(name()=="description")
- read_description();
- qDebug()<<"Nowattheendofsubexception...";
- voidxml_reader::read_description()
- qDebug()<<"descriptionis:"<<readElementText();
- voidxml_reader::read_duration()
- qDebug()<<"durationis:"<<readElementText();
- voidxml_reader::read_event()
- qDebug()<<"Starttoreadevent...";
- if(name()=="id")
- read_id();
- if(name()=="offsettime")
- read_offsettime();
- if(name()=="eventdesc")
- read_eventdesc();
- if(name()=="lasthandle")
- read_lasthandle();
- qDebug()<<"Nowattheendofevent..";
- voidxml_reader::read_offsettime()
- qDebug()<<"offsettimeis:"<<readElementText();
- voidxml_reader::read_eventdesc()
- qDebug()<<"Starttoreadeventdesc...";
- if(name()=="eventname")
- read_eventname();
- if(name()=="prompt")
- read_prompt();
- qDebug()<<"Nowattheendofeventdesc...";
- voidxml_reader::read_eventname()
- qDebug()<<"eventnameis:"<<readElementText();
- voidxml_reader::read_prompt()
- qDebug()<<"promptis:"<<readElementText();
- voidxml_reader::read_lasthandle()
- qDebug()<<"lasthandleis:"<<readElementText();
- }