Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QXmlStreamReader - Get XML file attributes



  • I'm trying to get the attributes from an XML file using QXmlStreamReader but I can't find the right method to do it, here is my code:

    C++

    QFile file("file.xml");
        file.open(QFile::ReadWrite);
        QXmlStreamReader reader(&file);
        while(reader.readNextStartElement()){
            while(reader.readNextStartElement()){
                if(reader.name() == "anotation"){
                    qDebug() << reader.qualifiedName();     //Returns the tag name
                    qDebug() << reader.readElementText();   //Returns the text inside the tag
                    //Here i need to get the tag parameters
                }
            }
        }
    

    And here is my file, from where I'm trying to get the values from frame_ini and frame_fin:
    XML

    <data>
    	<anotations>
    		<anotation frame_ini = "0" frame_fin = "0"> Anotação teste de frame único </anotation>
    		<anotation frame_ini = "10" frame_fin = "14"> Anotação do frame 10 ao 14 </anotation>
    		<anotation frame_ini = "15" frame_fin = "15"> Esta aparece apenas no frame 15 </anotation>
    		<anotation frame_ini = "20" frame_fin = "30"> Do frame 20 ao 30 existe esta anotação </anotation>
    		<anotation frame_ini = "31" frame_fin = "31"> A anotação atual só será exibida no frame 31 </anotation>
    	</anotations>
    </data>
    

    SOLVED
    If anyone is also having this problem, here is the solution:
    C++

    reader.attributes().value("frame_ini");
    


  • Hi,
    Please consider this link for parsing xml.



  • @TobbY said in QXmlStreamReader - Get XML file attributes:

    Hi,
    Please consider this link for parsing xml.

    Hi,
    Thanks for the answer!
    I'm reading it right now, but it's really complex. I guess there might be an easier way to get that values.

    UPDATE
    I've found this method:

    reader.attributes().length();
    

    It returns "2", that is the exact number of parameters from the annotations, but when I try to get the values it returns me this error:
    C++

    qDebug() << reader.attributes().first().value();
    

    This is the line from the code that tries to get the value from the first parameter.

    The inferior stopped because it received a signal from the operating system.
    
    Signal name : 
    SIGABRT
    Signal meaning : 
    Aborted
    
    

    UPDATE:

    Got it!
    I need to use the qualifiedName from the attribute i want to reach, like this:
    C++

    reader.attributes().value("frame_ini");
    

Log in to reply