Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QXmlStreamReader - Get XML file attributes

QXmlStreamReader - Get XML file attributes

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Iago.B.Almeida
    wrote on 17 Sept 2018, 12:14 last edited by Iago.B.Almeida
    #1

    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");
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      TobbY
      wrote on 17 Sept 2018, 12:30 last edited by
      #2

      Hi,
      Please consider this link for parsing xml.

      1 Reply Last reply
      1
      • I Offline
        I Offline
        Iago.B.Almeida
        wrote on 17 Sept 2018, 12:47 last edited by Iago.B.Almeida
        #3

        @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");
        
        1 Reply Last reply
        1

        1/3

        17 Sept 2018, 12:14

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved