Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2320056
  • 博文数量: 318
  • 博客积分: 8752
  • 博客等级: 中将
  • 技术积分: 4944
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-23 07:56
文章分类

全部博文(318)

文章存档

2019年(1)

2017年(2)

2016年(12)

2015年(2)

2014年(1)

2013年(17)

2012年(22)

2011年(9)

2010年(37)

2009年(33)

2008年(44)

2007年(43)

2006年(95)

分类: 其他平台

2013-02-05 17:51:01

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

源代码:

xml_reader.h

#ifndef XML_READER_H
#define XML_READER_H
#include
//#include
class xml_reader : public QXmlStreamReader
{
    //Q_OBJECT
public:
    xml_reader();
    bool read(QIODevice *device);
private:
    void read_data();
    void read_startweek();
    void read_eventnumber();
    void read_period();
    void read_id();
    void read_starttime();
    void read_endtime();
    void read_exception();
    void read_subexception();
    void read_description();
    void read_duration();
    void read_event();
    void read_offsettime();
    void read_eventdesc();
    void read_eventname();
    void read_prompt();
    void read_lasthandle();
    void read_unknow();
};
#endif // XML_READER_H

xml_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



2010-03-01 00:00:00.000
2

223401
2010-03-01 00:00:00.000
2010-06-20 23:59:59.999


01
2010-04-03 00:00:00.000
2010-04-05 23:59:59.000
清明节放假三天


02
2010-05-01 00:00:00.000
2010-05-03 23:59:59.000
五一劳动节放假三天


0000-00-07 00:00:00.000

01
0000-00-00 14:00:00.000

编译原理
ring

null


02
0000-00-03 09:50:00.000

编译原理
vibration

null


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


发现用xread.name().toString()可以把节点名读出来
要读节点中的属性怎么读出来啊?

attributes()返回的是QVector.的派生类QXmlStreamAttributes,有方法value()来返回属性值。
比如attributes().at(i).vaue("xxxx").toString()

attributes().at(i).name()


阅读(7178) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~