天天看点

QT SAX读取XML

XML,Extensible Markup Language,

可拓展标记语言,它可以用来标记数据、定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言。要求所有的标记必须成对出现且区分大小写。

xml学习网站:​​​http://www.w3school.com.cn/x.asp​​​

先用QT创建一个简单的XML文件。相关的类有QDomDocument,QDomElement。

#include <QtXml>
#include <QtCore>

int main(int argc,char **argv){
    QCoreApplication app(argc,argv);
    QDomDocument doc;
    QDomElement root = doc.createElement("root");
    doc.appendChild(root);
    for(qint8 i=0;i<4;i++){
        QDomElement book = doc.createElement("book");
        book.setAttribute("name","book "+QString::number(i+1));
        root.appendChild(book);
        for(qint8 j=0;j<5;j++){
            QDomElement chapter = doc.createElement("Chapter");
            chapter.setAttribute("name","chapter "+QString::number(j+1));
            chapter.setAttribute("id",j+1);
            book.appendChild(chapter);
        }
    }
    QFile file(QCoreApplication::applicationDirPath()+"/test.xml");
    if(file.open(QFile::WriteOnly | QFile::Text)){
        QTextStream in(&file);
        in<<doc.toString();
        file.flush();
        file.close();
        qDebug()<<"finished.";
    }
    else qDebug()<<"file open failed.";
    return app.exec();      

用火狐浏览器打开:

SAX是The simple API for XML的缩写,用于读取XML文件内容,和DOM相比,文件内容不用一次性全部加载到内存中。相关的处理类有三个: QXmlInputSource, QXmlSimpleReader以及一个 自定义类handler。

用SAX来阅读刚刚书写的xml文件:

handler.h:

注: 在本例handler类中, 四个bool函数不是必须全部都得写,startElement是必需的。其他3个可不写。

#ifndef HANDLER_H
#define

#include <QXmlDefaultHandler>

class Handler : public QXmlDefaultHandler
{
public:
    Handler();
    bool startDocument();
    bool endDocument();
    bool startElement(const QString &namespaceURI, const QString &localName, \
                      const QString &qName, const QXmlAttributes &atts);
    bool endElement(const QString &namespaceURI, const QString &localName, const QString &qName);
};

#endif      

handler.cpp

#include "handler.h"
#include <QDebug>

Handler::Handler()
{
}

bool Handler::startDocument()
{
    //qDebug()<<"start doc";
    return true;
}

bool Handler::endDocument()
{
    //qDebug()<<"end doc";
    return true;
}

bool Handler::startElement(const QString &namespaceURI, const QString &localName, \
                           const QString &qName, const QXmlAttributes &atts)
{
    //if(atts.length()>1) qDebug()<<"start of element";
    for(qint8 i=0;i<atts.length();i++){
        QByteArray array1 = atts.qName(i).toLatin1();
        QByteArray array2 =atts.value(i).toLatin1();
        char *s1 = array1.data();
        char *s2 = array2.data();
        //printf(" %s = %s ",QString((QLatin1Char *)atts.qName(i)),QString((QLatin1Char *)atts.value(i)));
        printf(" %s = %s ",s1,s2);
    }
    printf("\n");
    return true;
}

bool Handler::endElement(const QString &namespaceURI, const QString &localName, const QString &qName)
{
    //if(atts.length()>1) qDebug()<<"end of element";
    return true;
}      
#include <QXmlInputSource>
#include <QtCore>
#include "handler.h"

int main(int argc,char **argv){
    QFile file("/home/edemon/workspace/my_qt/myxml/test.xml");
    if(!file.open(QFile::ReadOnly | QFile::Text)){
        qDebug("open file for reading failed");
        return -1;
    }

    QXmlInputSource source(&file);
    Handler handler;
    QXmlSimpleReader reader;
    reader.setContentHandler(&handler);
    reader.parse(source);
    file.close();
    return 0;
}      
name = book 1 
 id = 1  name = chapter 1 
 id = 2  name = chapter 2 
 id = 3  name = chapter 3 
 id = 4  name = chapter 4 
 id = 5  name = chapter 5 
 name = book 2 
 id = 1  name = chapter 1 
 id = 2  name = chapter 2 
 id = 3  name = chapter 3 
 id = 4  name = chapter 4 
 id = 5  name = chapter 5 
 name = book 3 
 id = 1  name = chapter 1 
 id = 2  name = chapter 2 
 id = 3  name = chapter 3 
 id = 4  name = chapter 4 
 id = 5  name = chapter 5 
 name = book 4 
 id = 1  name = chapter 1 
 id = 2  name = chapter 2 
 id = 3  name = chapter 3 
 id = 4  name = chapter 4 
 id = 5  name = chapter 5