Unsolved QXmlStreamReader ends up in an endless loop when tag names have ":" double points
-
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(); }
-
@RoadCoder
Putting a colon (:
) in an XML tag name mean it is in a namespace. Your<system:setup>
is an XML element namedsetup
in a namespace calledsystem
.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 theqDebug()
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.