... read xml files in Qt

This section shows how to read a prototype of xml file with motor information inside a Qt client using Qt libraries.

The xml file looks like:

<?xml version="1.0"?>
<hw>
  <motor>
    <name>mot1</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot1</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
  <motor>
    <name>mot2</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot2</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
  <motor>
    <name>mot3</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot3</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
  <motor>
    <name>mot4</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot4</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
  <motor>
    <name>mot5</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot5</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
  <motor>
    <name>mot6</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot6</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
  <motor>
    <name>mot7</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot7</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
  <motor>
    <name>mot8</name> <type>stepping_motor</type> <module>oms58</module>
    <device>bw4/exp/mot8</device> <control>tango</control> <hostname>has107k</hostname>
  </motor>
</hw>

And an example program for reading it:

#include <stdlib.h>
#include <QtXml>
#include <iostream>

using namespace std;

void getMotorInformation( const QDomElement &motor );

int main(int argc,char** argv){

	QDomDocument doc("online");
	QFile file("online.xml");
	if (!file.open(QIODevice::ReadOnly))
		return 0;
	if (!doc.setContent(&file)) {
		file.close();
		return 0;
	}
	file.close();

// Take the elements that are children of the outermost element.

	QDomElement docElem = doc.documentElement();
    for(QDomNode n = docElem.firstChild(); !n.isNull(); n = n.nextSibling())
    {   
		if( !n.isNull() ) {
			if ( n.isElement() && n.nodeName() == "motor" ) {
				QDomElement motor = n.toElement();
				getMotorInformation( motor );
			}
		}
	}
	return 1;
}

void getMotorInformation( const QDomElement &motor )
{
    // visit all children of the motor element and take the info that is needed

    string name_for_motor;

	for(QDomNode n = motor.firstChild(); !n.isNull(); n = n.nextSibling())
	{ 
		if( !n.isNull() ) {
			if ( n.isElement() && n.nodeName() == "name" ) {
				QDomElement motor = n.toElement();
				QString text_my = motor.text();				
				name_for_motor = text_my.toStdString();
                cout << "Debug: name " << name_for_motor << endl;
                if(name_for_motor == "mot1") cout << "Debug: mot1 is the name " << endl;
			}
		}

	}  
}

This code can be introduced in a standard Qt client.