How to read the attributes of xml tags using QXmlStreamReader?

1

I am working with xml files, using the QT Liberia. The problem that presents itself to me is as follows: I do not know how to read the attributes of xml tags using QXmlStreamReader.

For example I have the following file:

<?xml version="1.0" encoding="UTF-8"?>
<wells>
    <number-wells value="3"/>   
    <wells-info>
        <row well-number="1" nodes="2" well-name="0.00000" Krg="">
        <row well-number="1" nodes="2" well-name="0.00000" Krg="">      
        <row well-number="1" nodes="2" well-name="0.00000" Krg="">
    </wells-info>
    <wells-direction>
        <row well-number="1" i="5" j="5" k="1" idir="1">
        <row well-number="2" i="5" j="5" k="1" idir="1">
        <row well-number="3" i="5" j="5" k="1" idir="1">
    </wells-direction>      
</wells>

According to what I was reading on some sides, I suggested the following:

xml.setDevice(&archivo);
QXmlStreamAttributes atributos;
while(!xml.atEnd()){
    qDebug() << xml.readNext();
    if(xml.isStartElement()){
        QString etiqueta = xml.name().toString();
        if(etiqueta == "number-wells"){
            atributos = xml.attributes();
            foreach(const QXmlStreamAttribute &att, atributos) {
                qDebug() << "atributo" << att.name() << att.value();
            }
         }
         if(etiqueta == "wells-info"){
            qDebug() << "etiqueta: " << etiqueta;
            while(xml.readNextStartElement()) {
                if(etiqueta == "row"){
                    qDebug() << "etiqueta: " << etiqueta;
                    atributos = xml.attributes();
                    foreach(const QXmlStreamAttribute &att, atributos) {
                        qDebug() << "atributo" << att.name() << att.value();
                    }
                }
                else xml.skipCurrentElement();
            }
        }
    }
    xml.readNext();
} //end WHILE*/
if(xml.hasError()){
    QMessageBox::critical(this,"Error","No se ha podido leer el archivo "+fileNameWell);
}

You're just showing me the following:

  

2

     

6

     

5

     

4

     

tag: "wells-info"

     

QXcbConnection: XCB error: 3 (BadWindow), sequence: 986, resource id:   17307926, major code: 40 (TranslateCoords), minor code: 0

What can it be?

    
asked by marcos vasquez 02.06.2017 в 15:12
source

1 answer

0
if(xml.isEndElement())
  { xml.readNext(); }
xml.readNext();

That is, if you are in the closing tag you go to the next ... What will be the opening tag ... then go to the next tag ... I would tell you that you can remove the% full if .

EDITO

Taken from the documentation of the method readNextStartElement of class QXmlStreamReader :

  

The current element is the element matching the most recently parsed start element of which a matching end element has not yet reached. When the parser has reached the end element, the current element becomes the parent element.

Summing up:

  

This method searches for the end of the current element and then searches for the next element.

You pretend to read the list of children and this method will not serve you at all. You have to iterate in a similar way to how you do in the first level:

if(etiqueta == "wells-info"){
    qDebug() << "etiqueta: " << etiqueta;
    while(xml.readNext()) {
        QString etiqueta = xml.name().toString();
        if(etiqueta == "row" && xml.isStartElement() )
        {
            qDebug() << "etiqueta: " << etiqueta;
            atributos = xml.attributes();
            foreach(const QXmlStreamAttribute &att, atributos) {
                qDebug() << "atributo" << att.name() << att.value();
            }
        }
        if(etiqueta == "wells-info")
            break;
    }
}
    
answered by 02.06.2017 / 21:28
source