Qt的XML模块包含了两种处理xml的API,分别是SAX和DOM。SAX提供了一种低层的基于事件驱动的方式解析xml文件,而DOM提供了一种高层的接口用于解析xml文件。不同之处在于DOM受制于内存的大小,一次不能解析文件大小太大的xml文件,因为它将xml文件一次性地读入内存中。
SAX方式解析XML文件
SAX方式解析XML是基于事件方式编程的。执行的流程完全取决于读入的数据。使用该方式的主要步骤是创建一个XML读取器以及一个处理器,然后将两者关联起来,之后调用parse(),涉及的各方用类图表示如下:
代码示例如下:
myhandler.h
- #ifndefMYHANDLER_H
- #defineMYHANDLER_H
- #include<QXmlDefaultHandler>
- classQString;
- classMyHandler:publicQXmlDefaultHandler
- {
- public:
- explicitMyHandler();
- signals:
- publicslots:
- boolstartDocument();
- boolstartElement(constQString&namespaceURI,
- constQString&localName,
- constQString&qName,153); background-color:inherit; font-weight:bold">constQXmlAttributes&atts);
- boolcharacters(constQString&text);
- boolendElement(constQString&qName);
- private:
- QStringindent;
- };
- #endif//MYHANDLER_H
myhandler.cpp
#include"myhandler.h"
- #include<QObject>
- #include<QTextStream>
- #include<QDebug>
- MyHandler::MyHandler():QXmlDefaultHandler()
- }
- boolMyHandler::startDocument(){
- indent="";
- returnTRUE;
- boolMyHandler::characters(constQString&text){
- QStringt=text;
- qDebug()<<t.remove('n');
- returnTRUE;
- }
- boolMyHandler::startElement(constQString&,153); background-color:inherit; font-weight:bold">constQXmlAttributes&atts){
- QStringstr=QString("n%1%2").arg(indent).arg(qName);
- qDebug()<<str;
- if(atts.length()>0){
- QStringfieldName=atts.qName(0);
- QStringfieldValue=atts.value(0);
- qDebug()<<QString("(%2=%3)").arg(fieldName).arg(fieldValue);
- qDebug()<<"{";
- indent+="";
- boolMyHandler::endElement(constQString&){
- indent.remove(0,4);
- qDebug()<<"}";
- }
main.cpp
#include<QApplication>
- #include<QFile>
- #include<QXmlInputSource>
- #include"myhandler.h"
- #include<QDebug>
- intmain(intargc,char*argv[])
- {
- QApplicationa(argc,argv);
- QFilexmlFile(":/test.xml");
- QXmlInputSourcesource(&xmlFile);
- if(!xmlFile.open(QFile::ReadOnly|QFile::Text)){
- return-1;
- MyHandlerhandler;
- QXmlSimpleReaderreader;
- reader.setContentHandler(&handler);
- if(reader.parse(source))
- qDebug()<<"Fileloaded!";
- returna.exec();
- DOM方式解析DOM方式只适合解析小的xml文件,它是基于SAX之上的一种高层解析XML方法。使用方式见如下代码所示:
#include<QtCore/QCoreApplication>
- #include<QDomDocument>
- #include<QFile>
- voidparseFolderElement(constQDomElement&element)
- QDomElementchild=element.firstChildElement();
- while(!child.isNull()){
- if(child.tagName()=="folder"){
- parseFolderElement(child);
- }elseif(child.tagName()=="bookmark"){
- QStringtitle=child.firstChildElement("title").text();
- if(title.isEmpty())
- title=QObject::tr("Folder");
- qDebug()<<"title="<<title;
- }if(child.tagName()=="separator"){
- qDebug()<<"processingseparator";
- child=child.nextSiblingElement();
- voidparseXmlByDom(QIODevice*device)
- QDomDocumentdomDocument;
- QStringerrorStr;
- interrorLine;
- interrorColumn;
- if(!domDocument.setContent(device,true,&errorStr,&errorLine,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> &errorColumn)){
- qDebug()<<"Parseerroratline"
- <<errorLine
- <<",column"
- <<errorColumn
- <<":n"
- <<errorStr;
- return;
- QDomElementroot=domDocument.documentElement();
- if(root.tagName()!="xbel"){
- qDebug()<<QString("ThefileisnotanXBELfile.");
- return;
- if(root.hasAttribute("version")
- &&root.attribute("version")!="1.0"){
- qDebug()<<QString("ThefileisnotanXBELversion1.0file.");
- QDomElementchild=root.firstChildElement("folder");
- parseFolderElement(child);
- child=child.nextSiblingElement("folder");
- char*argv[])
- QCoreApplicationa(argc,argv);
- QFilef("frank.xbel");
- if(!f.open(QIODevice::ReadOnly))
- qDebug()<<"failedtoopenfile";
- parseXmlByDom(&f);
- }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|