// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
#include <iostream>

using namespace std;

#include <QtCore/QFile>
#include <QtCore/QXmlStreamReader>

const char *XML_FILE = "employees.xml";

void realmain()
{
    // open file
    QFile file(XML_FILE);
    file.open(QIODevice::ReadOnly);
    QXmlStreamReader xsr(&file);
    // process file
    QString buf = "";
    while(!xsr.atEnd()) {
        xsr.readNext();
        switch(xsr.tokenType())
        {
            // accumulate text (note: can be entered multiple times for a text node)
            case QXmlStreamReader::Characters:
                buf += xsr.text();
                break;
            case QXmlStreamReader::StartElement:
                // if employee element find no and print
                if(xsr.name() == "employee")
                {
                    string no = xsr.attributes().value("no").toString().toStdString();
                    cout << "no = " << no << endl;
                }
                if(xsr.name() == "name" || xsr.name() == "role")
                {
                    buf = "";
                }
                break;
            case QXmlStreamReader::EndElement:
                // if sub elements name or role stop accumulating and print
                if(xsr.name() == "name")
                {
                    string name = buf.toStdString();
                    cout << "name = " << name << endl;
                }
                if(xsr.name() == "role")
                {
                    string role = buf.toStdString();
                    cout << "role = " << role << endl;
                }
                break;
        }
    }
    //
    file.close();
}
