The Extensible Markup Language(XML) can be read by humans and machines, it requires all markups must be paired and case sensitive.
The article shows how to write and read the XML file in the Qt environment.

Write Data To The Local XML File

Create a DOM element by QDomDocument object and set different attributes and relevant values for it.
The QDomDocument object can be put into another QDomDocument object, they have a father-son relationship after that.
Finally, convert the QDomDocument object’s data to the text stream and save it to the local XML file.

#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( "/Users/weiyang/Desktop/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();
}

Output:

Read The XML File

The simple API for XML(SAX) can be used to read the local XML file, its relevant working classes are QXmlInputSource, QXmlSimpleReader, and handler class which inherits QXmlDefaultHandler.
It’s worth noticing that we need not input the details of the task for every read function. I implemented four functions in the subclass.

main.cpp

#include <QXmlInputSource>
#include <QtCore>
#include "handler.h"

int main(int argc,char **argv){
    QFile file("/Users/weiyang/Desktop/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;
}

handler.h

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

Output:

 name = book 1 
 name = chapter 1  id = 1 
 name = chapter 2  id = 2 
 name = chapter 3  id = 3 
 name = chapter 4  id = 4 
 name = chapter 5  id = 5 
 name = book 2 
 name = chapter 1  id = 1 
 name = chapter 2  id = 2 
 name = chapter 3  id = 3 
 name = chapter 4  id = 4 
 name = chapter 5  id = 5 
 name = book 3 
 name = chapter 1  id = 1 
 name = chapter 2  id = 2 
 name = chapter 3  id = 3 
 name = chapter 4  id = 4 
 name = chapter 5  id = 5 
 name = book 4 
 name = chapter 1  id = 1 
 name = chapter 2  id = 2 
 name = chapter 3  id = 3 
 name = chapter 4  id = 4 
 name = chapter 5  id = 5 
Categories: CPlusPlusQt

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
A prohibited operation
0
Would love your thoughts, please comment.x
()
x