天天看點

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