1

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?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Is it possible to read XML file, which was written with QXMLStreamWriter using QDomDocument? I can successfully write this file with QXMLStreamWriter (and I did it) but have problems with reading only ...
Yes, should be possible. But it's far more nasty than using the QDomDocument. Does the qDebug() tell you what you expect ? You can qDebug() the sizes of the nested company vectors to check whether they grow as expected after the closing brace of the if.
Also note that your code would only work with same number of tasks for all the companies. I would include a layer where you check for the Company Tags to increment j and set i back to 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.