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

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 last edited by DoubleC122
    #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.

    kshegunovK 1 Reply Last reply
    0
    • D DoubleC122

      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.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on 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
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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 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
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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 last edited by
              #5

              I did, didn't print any error.

              1 Reply Last reply
              0
              • D DoubleC122

                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.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on 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 last edited by
                  #7

                  That fixed the problem, thank you.

                  1 Reply Last reply
                  0

                  • Login

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