PDA

View Full Version : Access elements of the xml-file



8Observer8
8th January 2013, 12:24
Hi, everyone!

I read the xml-file:



<Matrix>
<Description>
<rows>3</rows>
<columns>4</columns>
</Description>
<row1>
<cell>23.7</cell>
<cell>2.7</cell>
<cell>3.7</cell>
<cell>23.9</cell>
</row1>
<row2>
<cell>13.0</cell>
<cell>2.7</cell>
<cell>3.7</cell>
<cell>2</cell>
</row2>
<row3>
<cell>2.5</cell>
<cell>2.7</cell>
<cell>3.7</cell>
<cell>23.9</cell>
</row3>
</Matrix>


I want to write to the console these names of tags 'row1' and 'row2':



qDebug() << rowElement1.tagName();
qDebug() << rowElement2.tagName();


I expect to see:


"row1"
"row2"


But I see:


"row1"
"row1"




#include <QApplication>

#include <QFile>
#include <QTextStream>

#include <QDomDocument>
#include <QDomElement>
#include <QDomText>

#include <QMessageBox>

#include <QDebug>
#include <QObject>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QFile file( "matrix.xml" );
if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QMessageBox::critical(NULL, QObject::tr("Error"), QObject::tr("Failed to open file for reading"));
return 0;
}

QDomDocument document;
if( !document.setContent( &file ) )
{
QMessageBox::critical(NULL, QObject::tr("Error"), QObject::tr("Failed to parse the file into a DOM tree"));
file.close();
return 0;
}

file.close();

QDomElement matrixElement = document.documentElement();

QDomNode descriptionNode = matrixElement.firstChild();

QDomElement rowElement1 = descriptionNode.nextSiblingElement();
qDebug() << rowElement1.tagName();

QDomElement rowElement2 = descriptionNode.nextSiblingElement();
qDebug() << rowElement2.tagName();

app.exec();
}


Thanks!

Ivan

Lykurg
8th January 2013, 12:48
QDomElement rowElement2 = rowElement1.nextSiblingElement();
qDebug() << rowElement2.tagName();

8Observer8
8th January 2013, 19:27
Thank you very much!