QXmlStreamReader::readNext() Call Doesn't Read Next Element
-
wrote on 7 Aug 2022, 20:13 last edited by
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.
wrote on 7 Aug 2022, 20:35 last edited by@Crag_Hack
Print out the QXmlStreamReader::TokenType returned by eachreadNext()
. If it'sQXmlStreamReader::Characters
, checkisWhitespace()
.Maybe you should look at readNextStartElement() rather than
readNext()
. -
wrote on 7 Aug 2022, 20:44 last edited by Crag_Hack 8 Jul 2022, 20:47
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?
wrote on 7 Aug 2022, 20:50 last edited by JonB 8 Jul 2022, 20:52@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. -
wrote on 7 Aug 2022, 21:03 last edited by Crag_Hack 8 Jul 2022, 21:03
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(); }
wrote on 7 Aug 2022, 22:49 last edited by JonB 8 Jul 2022, 22:57@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()
.
1/6