QT SAX读取XML

25 篇文章 1 订阅

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 HANDLER_H

#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_H

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;
}

main.cpp

#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 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值