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 ends up in an endless loop when tag names have ":" double points
Qt 6.11 is out! See what's new in the release blog

QXmlStreamReader ends up in an endless loop when tag names have ":" double points

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 452 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.
  • R Offline
    R Offline
    RoadCoder
    wrote on last edited by
    #1

    Hello,
    i have a problem with QXmlStreamReader. I have existing xml files for configuration
    and i try to parse these files. Does anyone know why? I can't find the reason. I have
    prepared a minimal example with two xml files and a main.cpp. With ExampleB.xml
    the code works fine, with ExampleA.xml the code ends in an endless loop in line 6 of
    the xml file.

    ExampleA.xml (with double points in tag names)

    <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
    <config>
    	<metadata>
    		<name>Configuration name</name>
    		<calibration>
    			<system:setup>Setup></system:setup>
    			<system:min>Min value></system:min>
    			<system:max>Max value></system:max>
    			<system:spi>SPI config></system:spi>
    			<system:i2c>I2C config></system:i2c>
    		</calibration>
    	</metadata>
    </config>
    

    ExampleB.xml (without double points in tag names)

    <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
    <config>
    	<metadata>
    		<name>Configuration name</name>
    		<calibration>
    			<system_setup>Setup></system_setup>
    			<system_min>Min value></system_min>
    			<system_max>Max value></system_max>
    			<system_spi>SPI config></system_spi>
    			<system_i2c>I2C config></system_i2c>
    		</calibration>
    	</metadata>
    </config>
    

    main.cpp

    #include <QCoreApplication>
    #include <QFile>
    #include <QXmlStreamReader>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // QFile file("d:/Stefan/Documents/Qt/xmlbug/ExampleA.xml");
        QFile file("d:/Stefan/Documents/Qt/xmlbug/ExampleB.xml");
    
        QXmlStreamReader reader;
    
        // Open File
        if( file.open(QFile::ReadOnly | QFile::Text) )
        {
            // Parse through file
            reader.setDevice(&file);
    
            while ( (!reader.atEnd()) && (!reader.hasError()) )
            {
                  if(reader.tokenType() == QXmlStreamReader::StartElement)
                  {
                        if( reader.name().toString()=="calibration" )
                        {
                            QString tagName;
                            QString tagData;
    
                            while( !(reader.tokenType()==QXmlStreamReader::EndElement && reader.name().toString()=="calibration") )
                            {
                                if( reader.tokenType()==QXmlStreamReader::StartElement && reader.name().toString()!="calibration" )
                                {
                                    tagName = reader.name().toString();
                                    tagData = reader.readElementText();
                                    qDebug() << "Setup" << tagName << tagData;
                                }
    
                                reader.readNext();
                                qDebug() << reader.name().toString() << reader.lineNumber();
                            }
                        }
                  }
    
                  qDebug() << reader.name().toString() << reader.lineNumber();
                  reader.readNext();
            }
    
            qDebug() << "Close File";
    
            // Close File
            file.close();
        }
    
        return a.exec();
    }
    
    
    JonBJ 1 Reply Last reply
    0
    • R RoadCoder

      Hello,
      i have a problem with QXmlStreamReader. I have existing xml files for configuration
      and i try to parse these files. Does anyone know why? I can't find the reason. I have
      prepared a minimal example with two xml files and a main.cpp. With ExampleB.xml
      the code works fine, with ExampleA.xml the code ends in an endless loop in line 6 of
      the xml file.

      ExampleA.xml (with double points in tag names)

      <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
      <config>
      	<metadata>
      		<name>Configuration name</name>
      		<calibration>
      			<system:setup>Setup></system:setup>
      			<system:min>Min value></system:min>
      			<system:max>Max value></system:max>
      			<system:spi>SPI config></system:spi>
      			<system:i2c>I2C config></system:i2c>
      		</calibration>
      	</metadata>
      </config>
      

      ExampleB.xml (without double points in tag names)

      <?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
      <config>
      	<metadata>
      		<name>Configuration name</name>
      		<calibration>
      			<system_setup>Setup></system_setup>
      			<system_min>Min value></system_min>
      			<system_max>Max value></system_max>
      			<system_spi>SPI config></system_spi>
      			<system_i2c>I2C config></system_i2c>
      		</calibration>
      	</metadata>
      </config>
      

      main.cpp

      #include <QCoreApplication>
      #include <QFile>
      #include <QXmlStreamReader>
      #include <QDebug>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          // QFile file("d:/Stefan/Documents/Qt/xmlbug/ExampleA.xml");
          QFile file("d:/Stefan/Documents/Qt/xmlbug/ExampleB.xml");
      
          QXmlStreamReader reader;
      
          // Open File
          if( file.open(QFile::ReadOnly | QFile::Text) )
          {
              // Parse through file
              reader.setDevice(&file);
      
              while ( (!reader.atEnd()) && (!reader.hasError()) )
              {
                    if(reader.tokenType() == QXmlStreamReader::StartElement)
                    {
                          if( reader.name().toString()=="calibration" )
                          {
                              QString tagName;
                              QString tagData;
      
                              while( !(reader.tokenType()==QXmlStreamReader::EndElement && reader.name().toString()=="calibration") )
                              {
                                  if( reader.tokenType()==QXmlStreamReader::StartElement && reader.name().toString()!="calibration" )
                                  {
                                      tagName = reader.name().toString();
                                      tagData = reader.readElementText();
                                      qDebug() << "Setup" << tagName << tagData;
                                  }
      
                                  reader.readNext();
                                  qDebug() << reader.name().toString() << reader.lineNumber();
                              }
                          }
                    }
      
                    qDebug() << reader.name().toString() << reader.lineNumber();
                    reader.readNext();
              }
      
              qDebug() << "Close File";
      
              // Close File
              file.close();
          }
      
          return a.exec();
      }
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @RoadCoder
      Putting a colon (:) in an XML tag name mean it is in a namespace. Your <system:setup> is an XML element named setup in a namespace called system.

      system would normally be defined as namespace via an earlier e.g.:

      <config xmlns:system="https://www.somewhere.com/something">
      

      You don't have that, I don't know whether system might be a recognised namespace, though I don't think so.

      A tag in a namespace may affect your parsing/searching, but it shouldn't make anything "hang"/"loop" endlessly. Unless your code does that, I haven't looked. If you are stuck in your while loop you can use the qDebug() output to show you what is going on.

      You can't use : in tag names of your own, if that is what you're asking.

      1 Reply Last reply
      2

      • Login

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