QXmlStreamReader::readNext() Call Doesn't Read Next Element
-
Hi I have a peculiar and (hopefully) simple question. I have a well formed XML file created by QXmlStreamWriter. For some odd reason it takes two readNext() calls to transition from one element to the next when parsing the file with QXmlStreamReader. Any ideas what I might be doing wrong?
Here is the relevant part of the XML file:
<?xml version="1.0" encoding="UTF-8"?> <jobSettings> <advancedJobs> <job name="Simple Job"> <simple>true</simple>
And here's the code:
if (xmlReader.name() == "job") { BackupJob job; job.setName(xmlReader.attributes().value("name").toString()); xmlReader.readNext(); xmlReader.readNext(); QString awef = xmlReader.name().toString();
If I only do one readNext() call awef is set to "". After a second call it is set to "simple". I'm flabbergasted!
Thanks for the help.
-
Hi I have a peculiar and (hopefully) simple question. I have a well formed XML file created by QXmlStreamWriter. For some odd reason it takes two readNext() calls to transition from one element to the next when parsing the file with QXmlStreamReader. Any ideas what I might be doing wrong?
Here is the relevant part of the XML file:
<?xml version="1.0" encoding="UTF-8"?> <jobSettings> <advancedJobs> <job name="Simple Job"> <simple>true</simple>
And here's the code:
if (xmlReader.name() == "job") { BackupJob job; job.setName(xmlReader.attributes().value("name").toString()); xmlReader.readNext(); xmlReader.readNext(); QString awef = xmlReader.name().toString();
If I only do one readNext() call awef is set to "". After a second call it is set to "simple". I'm flabbergasted!
Thanks for the help.
@Crag_Hack
Print out the QXmlStreamReader::TokenType returned by eachreadNext()
. If it'sQXmlStreamReader::Characters
, checkisWhitespace()
.Maybe you should look at readNextStartElement() rather than
readNext()
. -
Thanks JonB. The blank name readNext() call is type 6 QXmlStreamReader::Characters and true for isWhiteSpace().
Why is readNext() reading whitespace? This isn't desirable behavior is it?
Is the only way to bypass this issue by using readNextStartElement() or testing for whitespace and executing an additional readNext() call in the case of whitespace?
-
Thanks JonB. The blank name readNext() call is type 6 QXmlStreamReader::Characters and true for isWhiteSpace().
Why is readNext() reading whitespace? This isn't desirable behavior is it?
Is the only way to bypass this issue by using readNextStartElement() or testing for whitespace and executing an additional readNext() call in the case of whitespace?
@Crag_Hack said in QXmlStreamReader::readNext() Call Doesn't Read Next Element:
The blank name readNext() call is type 6 is QXmlStreamReader::Characters and true for isWhiteSpace().
:-)
Why is readNext() reading whitespace? This isn't desirable behavior is it?
Because it's an XML reader, and that is the next token. Which is not necessarily the next element!
Is the only way to bypass this issue by using readNextStartElement() or testing for whitespace and executing an additional readNext() call in the case of whitespace?
Indeed. But use
readNextStartElement()
, that's what it's there for. Normally for "high level" purposes you are only interested in reading elements. You don't care about whitespace or comments etc. This is not special to Qt, you will find the same distinction in other XML reader/writers. -
Awesome JonB got thigs working thanks. My code was already catered to readNext() call so I replaced all readNext() calls with:
void Program::readNextXMLElement(QXmlStreamReader &xmlReader) { xmlReader.readNext(); while (xmlReader.isWhitespace()) xmlReader.readNext(); }
-
Awesome JonB got thigs working thanks. My code was already catered to readNext() call so I replaced all readNext() calls with:
void Program::readNextXMLElement(QXmlStreamReader &xmlReader) { xmlReader.readNext(); while (xmlReader.isWhitespace()) xmlReader.readNext(); }
@Crag_Hack
You should not need to callreadNext()
. You should not need to callisWhitespace()
. You should only need to callreadNextStartElement()
. Did you try with that? YourreadNextXMLElement()
looks like a not-so-comprehensivereadNextStartElement()
.Implementation is at https://code.woboq.org/qt6/qtbase/src/corelib/serialization/qxmlstream.cpp.html#_ZN16QXmlStreamReader20readNextStartElementEv
bool QXmlStreamReader::readNextStartElement() { while (readNext() != Invalid) { if (isEndElement()) return false; else if (isStartElement()) return true; } return false; }
OIC, you wrote:
My code was already catered to readNext() call
So you mean what you have is designed around that, for right or for wrong. I think you should rename your
readNextXMLElement()
, toreadNextNonWhitespace()
.