Reputation: 3930
I have such an XML file:
"<?xml version="1.0"?>
<OrderDetails>
<Companies number_of_companies="2" number_of_tasks="4">
<Company id="1">
<Tasks>
<Task id="1" brutto="0.01" netto="0.00813008" euro="0.00192201"/>
<Task id="2" brutto="0.02" netto="0.0162602" euro="0.00384401"/>
<Task id="3" brutto="0.03" netto="0.0243902" euro="0.00576602"/>
<Task id="4" brutto="0.04" netto="0.0325203" euro="0.00768802"/>
</Tasks>
</Company>
<Company id="2">
<Tasks>
<Task id="1" brutto="50" netto="40.6504" euro="9.61003"/>
<Task id="2" brutto="60" netto="48.7805" euro="11.532"/>
<Task id="3" brutto="70" netto="56.9106" euro="13.454"/>
<Task id="4" brutto="80" netto="65.0407" euro="15.376"/>
</Tasks>
</Company>
</Companies>
</OrderDetails>"
I would like to read brutto/netto/euro values to QVector<QVector<double> > brutto
so each company has its own costs in appropriate vector, I mean, I would like to have something like this:
brutto[0] = [0.01, 0.02, 0.03, 0.04, 0.05]
brutto[1] = [50, 60, 70, 80]
I tried with this code:
QVector<QVector<double> > readBruttoCostsFromXML(QString xml)
{
QXmlStreamReader xmlReader(xml);
int tasks = this->readNumOfTasksFromXML(xml);
int companies = this->readNumOfCompaniesFromXML(xml);
QVector<QVector<double> > bruttoCosts(companies);
int i=0, j = 0;
while(!xmlReader.atEnd() && !xmlReader.hasError()){
xmlReader.readNext();
if (xmlReader.name() == "Task" && xmlReader.isStartElement() && !xmlReader.isWhitespace())
{
bruttoCosts[j].push_back(xmlReader.attributes().value("brutto").toDouble());
i ++;
if(i == tasks-1)
{
i = 0;
j++;
}
qDebug() << bruttoCosts[i] << "\n";
}
}
return bruttoCosts;
}
But I'm getting the error ASSERT failure in QVector<T>::operator[]: "index out of range",
when I try to use this vector of vectors further in my code. My question is how to properly read such an XML file?
Upvotes: 1
Views: 558
Reputation: 1191
I had similar problems with the QXMLStreamReader.
It is not so obvious what happens with children tags. I experienced that QXMLStreamReader
might just read over them and can only handle less complex XML structures. It is designed for XML STREAMS what is not what you have and thus, not what you need.
To get full and easy access to all elements you should use the QDomDocument
instead.
Upvotes: 2