[Qt-interest] QXmlStreamReader: Usage of readNextStartElement()?
David Bowman
David.Bowman at iongeo.com
Tue Dec 21 18:16:44 CET 2010
Oliver,
I believe readNextStartElement recursively goes down through the
elements, as bar does not have any children readNextStart will return
false. If you call skipCurrentElement on a childless element it should
jump it back up a level.
See the xmlstream bookmarks example in the qt docs.
Regards,
David
-----Original Message-----
From: qt-interest-bounces+david.bowman=iongeo.com at qt.nokia.com
[mailto:qt-interest-bounces+david.bowman=iongeo.com at qt.nokia.com] On
Behalf Of Oliver.Knoll at comit.ch
Sent: 21 December 2010 17:07
To: qt-interest at trolltech.com
Subject: [Qt-interest] QXmlStreamReader: Usage of
readNextStartElement()?
Hi,
I am having difficulties understanding the proper usage of
QXmlStreamReader#readNextStartElement():
When I have the following code snippet which should print out all
element names and attributes:
QXmlStreamReader streamReader(&file);
while (streamReader.readNextStartElement()) {
qDebug("name: %s", qPrintable(streamReader.name().toString()));
QXmlStreamAttributes attributes = streamReader.attributes();
foreach(QXmlStreamAttribute attribute, attributes) {
qDebug(" Attribute: name: %s, value: %s",
qPrintable(attribute.name().toString()),
qPrintable(attribute.value().toString()));
}
}
And I have e.g.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE screenie>
<foo version="1.0">
<bar enabled="true" bgcolor="#ffffff"/>
<foobar baz="42"/>
<rabarber>Some text</rabarber>
</foo>
I only get:
name: foo
Attribute: name: version, value: 1.0
name: bar
Attribute: name: enabled, value: true
Attribute: name: bgcolor, value: #ffffff
That is the parsing seems to stop after the 2nd element <bar .../>.
>From the Qt docs
http://doc.trolltech.com/4.7/qxmlstreamreader.html#readNextStartElement:
"Reads until the next start element within the current element. [...]The
current element is the element matching the most recently parsed start
element of which a matching end element has not yet been reached."
So let's assume that <foo> - the outermost "start element" as it seems
to me - is the "current element", should I not get also the <foobar />
and <rababer /> elements? What really twists my mind is that I /do/ get
the <bar> element, but why not the <foobar> and <rabarber> elements,
which are at the same level as <bar>? So the time when
readNextStartElement seems to stop is a mistery to me...
If on the other hand I iterate over the XML like this with readNext():
while ((tokenType = streamReader.readNext()) !=
QXmlStreamReader::EndDocument) {
if (tokenType == QXmlStreamReader::StartElement) {
qDebug("name: %s", qPrintable(streamReader.name().toString()));
QXmlStreamAttributes attributes = streamReader.attributes();
foreach(QXmlStreamAttribute attribute, attributes) {
qDebug(" Attribute: name: %s, value: %s",
qPrintable(attribute.name().toString()),
qPrintable(attribute.value().toString()));
}
}
I do get all elements as expected.
The only difference between my readNextStartElement() version and the
http://doc.trolltech.com/4.7/xml-streambookmarks.html example seems to
be that in the later they do a readElementText() and a
skipCurrentElement() in between (and this example seems to work for me,
at least with a simple *.xbel bookmark file. I have yet to try out a
more complex one, e.g. with several "folders" or so). But other than
that also this Qt example seems to simply iterate over the entire XML
with readNextStartElement().
So what am I doing wrong? Is this supposed to work like I assume, or is
this a bug? This is on Qt 4.7.1. Example code and test file attached.
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
This email and any files transmitted with it are confidential and are intended solely for the use of the individual or entity to whom they are addressed. If you are not the original recipient or the person responsible for delivering the email to the intended recipient, be advised that you have received this email in error, and that any use, dissemination, forwarding, printing, or copying of this email is strictly prohibited. If you received this email in error, please immediately notify the sender and delete the original.
More information about the Qt-interest-old
mailing list