Simple XML parsing in Qt

Quite often, you need to create a configuration in a project that can be easily changed without recompiling.



Especially if this program controls a certain device, and you need to create an action depending on the state of the device or after some time. This is where XML comes in.



There is a simple xml config:
<?xml version="1.0" encoding="UTF-8" ?>
<etaps>
	<etap id="1">
		<name></name>
		<firststage>1</firststage>
		<laststage>9</laststage>
		<pausestage>10</pausestage>
        <etappause>5</etappause>
	</etap>
	<etap id="2">
		<name></name>
		<firststage>10</firststage>
		<laststage>20</laststage>
		<pausestage>15</pausestage>
        <etappause>2</etappause>
	</etap>
	<etap id="3">
		<name></name>
		<firststage>21</firststage>
		<laststage>50</laststage>
		<pausestage>15</pausestage>
        <etappause>3</etappause>
	</etap>
</etaps>

      
      







Task: parse a simple file using Qt, namely QXmlStreamReader.



Open the config:



    QFile* file = new QFile("config.xml"); 
    if (!file->open(QIODevice::ReadOnly | QIODevice::Text))
    {
        emit Log(tr("  XML-"), LOG_LEVEL_ERROR);
        return false;
    }
    QXmlStreamReader xml(file);

      
      





Find the desired tag (etaps) and find the nested etap tag:



    while (!xml.atEnd() && !xml.hasError())
    {
        QXmlStreamReader::TokenType token = xml.readNext();
        if (token == QXmlStreamReader::StartDocument)
            continue;
        if (token == QXmlStreamReader::StartElement)
        {
            if (xml.name() == "etaps")
                continue;
            if (xml.name() == "etap")
                XMLConf.append(parseEtap(xml));
        }
    }

      
      





parse inside the etap tag and add everything to QMap <QString, QString>:



check if we are there:



    QMap<QString, QString> etap;
    if (xml.tokenType() != QXmlStreamReader::StartElement && xml.name() == "etap")
        return etap;

      
      





parse each tag nested in etap and add it to QMap:



    QXmlStreamAttributes attributes = xml.attributes();
    if (attributes.hasAttribute("id"))
        etap["id"] = attributes.value("id").toString();
    xml.readNext();
    while (!(xml.tokenType() == QXmlStreamReader::EndElement && xml.name() == "etap"))
    {
        if (xml.tokenType() == QXmlStreamReader::StartElement)
        {
            if (xml.name() == "name")
                addElementDataToMap(xml, etap);
            if (xml.name() == "firststage")
                addElementDataToMap(xml, etap);
            if (xml.name() == "laststage")
                addElementDataToMap(xml, etap);
            if (xml.name() == "pausestage")
                addElementDataToMap(xml, etap);
            if (xml.name() == "etappause")
                addElementDataToMap(xml, etap);
        }
        xml.readNext();
    }

      
      





adding to QMap



void addElementDataToMap(QXmlStreamReader& xml, QMap<QString, QString>& map) const
{
    if (xml.tokenType() != QXmlStreamReader::StartElement)
        return;
    QString elementName = xml.name().toString();
    xml.readNext();
    map.insert(elementName, xml.text().toString());
}


      
      





As a result, we get a QMap <QString, QString> with several elements, each of which contains two lines: the name of the tag and its value.



All Articles