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 does not parse XML attributes and attribute values
Forum Update on Monday, May 27th 2025

QXmlStreamReader does not parse XML attributes and attribute values

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.3k 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.
  • D Offline
    D Offline
    DoubleC122
    wrote on 8 May 2018, 20:13 last edited by DoubleC122 5 Aug 2018, 20:14
    #1

    Hi. So, my issue is quite simple I'd say, I'm just trying to parse an XML file with QXmlStreamReader. So far so good, except I can't parse attributes and their values. I can parse everything else, like tags and elements, which I did, but I can't parse these attributes.

    So, my code is something like this:

    QFile xml_file("burma14.xml");
    xml_file.open(QIODevice::ReadOnly | QIODevice::Text);

    QXmlStreamReader reader(&xml_file); 
    QDomDocument doc("burma14"); 
    
    while(!reader.atEnd()) 
    {
        reader.readNext(); 
    
        if (!reader.isStartElement()) { 
            continue;
        }
    
        QString text = reader.name().toString(); 
    
        if(text == QStringLiteral("vertex"))
        {
            this->v++;
            std::cout<<"Vertex: "<<this->v<<std::endl;
        }
    
        if(text == QStringLiteral("edge"))
        {
            std::cout<<"Edge to vertex: "<<reader.readElementText().toInt()<<std::endl;
    
            QXmlStreamAttributes attribs = reader.attributes();
            QString attr = attribs.value("cost").toString();
            std::cout<<"Cost: "<<attr.toStdString()<<std::endl;
    
        }
    
    }
    

    Everything you see here works except for this part:

    QXmlStreamAttributes attribs = reader.attributes();
    QString attr = attribs.value("cost").toString();
    std::cout<<"Cost: "<<attr.toStdString()<<std::endl;

    It simply returns nothing. Maybe I'm doing it wrong? Is there a better way to do it? Thanks in advance for your time.

    K 1 Reply Last reply 8 May 2018, 20:44
    0
    • D DoubleC122
      8 May 2018, 20:13

      Hi. So, my issue is quite simple I'd say, I'm just trying to parse an XML file with QXmlStreamReader. So far so good, except I can't parse attributes and their values. I can parse everything else, like tags and elements, which I did, but I can't parse these attributes.

      So, my code is something like this:

      QFile xml_file("burma14.xml");
      xml_file.open(QIODevice::ReadOnly | QIODevice::Text);

      QXmlStreamReader reader(&xml_file); 
      QDomDocument doc("burma14"); 
      
      while(!reader.atEnd()) 
      {
          reader.readNext(); 
      
          if (!reader.isStartElement()) { 
              continue;
          }
      
          QString text = reader.name().toString(); 
      
          if(text == QStringLiteral("vertex"))
          {
              this->v++;
              std::cout<<"Vertex: "<<this->v<<std::endl;
          }
      
          if(text == QStringLiteral("edge"))
          {
              std::cout<<"Edge to vertex: "<<reader.readElementText().toInt()<<std::endl;
      
              QXmlStreamAttributes attribs = reader.attributes();
              QString attr = attribs.value("cost").toString();
              std::cout<<"Cost: "<<attr.toStdString()<<std::endl;
      
          }
      
      }
      

      Everything you see here works except for this part:

      QXmlStreamAttributes attribs = reader.attributes();
      QString attr = attribs.value("cost").toString();
      std::cout<<"Cost: "<<attr.toStdString()<<std::endl;

      It simply returns nothing. Maybe I'm doing it wrong? Is there a better way to do it? Thanks in advance for your time.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 8 May 2018, 20:44 last edited by
      #6

      https://forum.qt.io/topic/78571/qxmlstreamreader-attributes-not-consistent

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      3
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 8 May 2018, 20:20 last edited by
        #2

        Hi,

        First strange thing: you are using a relative path which means that the file in question will likely not be where you expect it to be..

        Then you don't check whether the opening did succeed so you might trying to parse an empty document.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • D Offline
          D Offline
          DoubleC122
          wrote on 8 May 2018, 20:33 last edited by
          #3

          I see. I fixed those two issues, but the main problem seems to persist. Even though this way worked for others.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 8 May 2018, 20:39 last edited by
            #4

            Did you check whether there was an error with QXmlStreamReader::hasError ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • D Offline
              D Offline
              DoubleC122
              wrote on 8 May 2018, 20:44 last edited by
              #5

              I did, didn't print any error.

              1 Reply Last reply
              0
              • D DoubleC122
                8 May 2018, 20:13

                Hi. So, my issue is quite simple I'd say, I'm just trying to parse an XML file with QXmlStreamReader. So far so good, except I can't parse attributes and their values. I can parse everything else, like tags and elements, which I did, but I can't parse these attributes.

                So, my code is something like this:

                QFile xml_file("burma14.xml");
                xml_file.open(QIODevice::ReadOnly | QIODevice::Text);

                QXmlStreamReader reader(&xml_file); 
                QDomDocument doc("burma14"); 
                
                while(!reader.atEnd()) 
                {
                    reader.readNext(); 
                
                    if (!reader.isStartElement()) { 
                        continue;
                    }
                
                    QString text = reader.name().toString(); 
                
                    if(text == QStringLiteral("vertex"))
                    {
                        this->v++;
                        std::cout<<"Vertex: "<<this->v<<std::endl;
                    }
                
                    if(text == QStringLiteral("edge"))
                    {
                        std::cout<<"Edge to vertex: "<<reader.readElementText().toInt()<<std::endl;
                
                        QXmlStreamAttributes attribs = reader.attributes();
                        QString attr = attribs.value("cost").toString();
                        std::cout<<"Cost: "<<attr.toStdString()<<std::endl;
                
                    }
                
                }
                

                Everything you see here works except for this part:

                QXmlStreamAttributes attribs = reader.attributes();
                QString attr = attribs.value("cost").toString();
                std::cout<<"Cost: "<<attr.toStdString()<<std::endl;

                It simply returns nothing. Maybe I'm doing it wrong? Is there a better way to do it? Thanks in advance for your time.

                K Offline
                K Offline
                kshegunov
                Moderators
                wrote on 8 May 2018, 20:44 last edited by
                #6

                https://forum.qt.io/topic/78571/qxmlstreamreader-attributes-not-consistent

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                3
                • D Offline
                  D Offline
                  DoubleC122
                  wrote on 8 May 2018, 21:12 last edited by
                  #7

                  That fixed the problem, thank you.

                  1 Reply Last reply
                  0

                  1/7

                  8 May 2018, 20:13

                  • Login

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