分类: 其他平台
2013-02-05 17:51:01
XML文件由于其扩展性与兼容性的优点,被广泛用作软件和系统的配置文件。这里简要介绍一下QT下如何来解析XML文件。
源代码:
xml_reader.h
#ifndef XML_READER_Hxml_reader.cpp
#include "xml_reader.h"
xml_reader::xml_reader()
{}
bool xml_reader::read(QIODevice *device)
{
setDevice(device);
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "data")
{
read_data();
}
}
}
return !error();
}
void xml_reader::read_data()
{
qDebug()<<"Start to read data...";
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "period")
{
read_period();
}
if(name() == "startweek")
{
read_startweek();
}
if(name() == "eventnumber")
{
read_eventnumber();
}
}
if(isEndElement())
{
if(name() == "data")
{
qDebug()<<"Now at the end of data element...";
break;
}
}
}
}
void xml_reader::read_startweek()
{
qDebug()<<"startweek is: "<
}
void xml_reader::read_eventnumber()
{
qDebug()<<"eventNumber is: "<
}
void xml_reader::read_period()
{
qDebug()<<"Start to read period...";
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())
{
if(name() == "period")
{
qDebug()<<"Now at the end of period...";
break;
}
}
}
}
void xml_reader::read_id()
{
qDebug()<<"id is: " << readElementText();
}
void xml_reader::read_starttime()
{
qDebug()<<"starttime is: " << readElementText();
}
void xml_reader::read_endtime()
{
qDebug()<<"endtime is: " << readElementText();
}
void xml_reader::read_exception()
{
qDebug()<<"Start to read exception...";
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "subexception")
{
read_subexception();
}
}
if(isEndElement())
{
if(name() == "exception")
{
qDebug()<<"Now at the end of exception...";
break;
}
}
}
}
void xml_reader::read_subexception()
{
qDebug()<<"Start to read subexception...";
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "id")
{
read_id();
}
if(name() == "starttime")
{
read_starttime();
}
if(name() == "endtime")
{
read_endtime();
}
if(name() == "description")
{
read_description();
}
}
if(isEndElement())
{
if(name() == "subexception")
{
qDebug()<<"Now at the end of subexception...";
break;
}
}
}
}
void xml_reader::read_description()
{
qDebug()<<"description is: " << readElementText();
}
void xml_reader::read_duration()
{
qDebug()<<"duration is: " << readElementText();
}
void xml_reader::read_event()
{
qDebug()<<"Start to read event...";
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "id")
{
read_id();
}
if(name() == "offsettime")
{
read_offsettime();
}
if(name() == "eventdesc")
{
read_eventdesc();
}
if(name() == "lasthandle")
{
read_lasthandle();
}
}
if(isEndElement())
{
if(name() == "event")
{
qDebug()<<"Now at the end of event..";
break;
}
}
}
}
void xml_reader::read_offsettime()
{
qDebug()<<"offsettime is: " << readElementText();
}
void xml_reader::read_eventdesc()
{
qDebug()<<"Start to read eventdesc...";
while(!atEnd())
{
readNext();
if(isStartElement())
{
if(name() == "eventname")
{
read_eventname();
}
if(name() == "prompt")
{
read_prompt();
}
}
if(isEndElement())
{
if(name() == "eventdesc")
{
qDebug()<<"Now at the end of eventdesc...";
break;
}
}
}
}
void xml_reader::read_eventname()
{
qDebug()<<"eventname is: " << readElementText();
}
void xml_reader::read_prompt()
{
qDebug()<<"prompt is: " << readElementText();
}
void xml_reader::read_lasthandle()
{
qDebug()<<"lasthandle is: " << readElementText();
}
main.cpp
#include
#include "XmlPraser.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile file("praser.xml");
XmlPraser reader;
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"File has loaded...";
reader.read(&file);
}
else
{
qDebug()<<"No file to read...";
}
file.close();
qDebug()<<"Press ctrl+C to exit...";
return a.exec();
}
XML文件范例 praser.xml
由于是使用的QXmlStrramReader来进行解析,因而所有的节点都需要自己编写函数进行相应的处理。
发现用xread.name().toString()可以把节点名读出来
要读节点中的属性怎么读出来啊?
attributes()返回的是QVector
比如attributes().at(i).vaue("xxxx").toString()
attributes().at(i).name()