[Qt-interest] QXmlStreamReader: Usage of readNextStartElement() [SOLVED]?
Oliver.Knoll at comit.ch
Oliver.Knoll at comit.ch
Wed Dec 22 10:01:08 CET 2010
Constantin, David,
Thanks for your help!
On 2010-12-21 Constantin Constantin Makshin wrote:
> The general idea is:
> If you use readNextStartElement() and want to get to the next element
> on the same level, then before calling this function again use
> readElementText() if the current element may have text and
> skipCurrentElement() otherwise.
Yes, that makes sense, and that is also what I kind of assumed, but had trouble mapping to the description in the Qt docs. Especially the part which sais "When the parser has reached the end element, the current element becomes the parent element." gave me the impression that somehow the parser would resume, until the end element of the /first/ (outermost) element would be hit (the "first" element being the first current element when readNextStartElement() was first called... uh...)
Example (which is wrong, but just to illustrate my understanding of the Qt docs):
<current>
<- parsing is here
<foo>
<bar />
</foo>
<a>...</a>
<b />
</current>
Assume the <current> element would be the "first current element", and the parser would be at the position marked with "<-". Now we would do:
- readNextStartElement(): returns <foo>, "current element" becomes <foo> (previously: <current>)
- readNextStartElement(): returns <bar>, "current element" becomes <bar />
- readNextStartElement (): Since we just hit the end element of <bar />, the "current element" becomes
the parent element again, which is <foo>...
- ... and since we just hit </foo> end element, the "current element" becomes the parent element
again, which is <parent>, and hence <a> would be returned
- readNextElement(): same logic as before, </a> end element is hit, parent <current> becomes
again the "current element", so <b /> is returned
- ...
- Until finally we hit </current> end element, which is the end element of the /first/ (or outermost)
element and hence false would be finally returned
Anyway, I understand that the above understanding is wrong and that basically readNextStartElement will just try to find the next start element, but will stop as soon as it hits /any/ end element (which is /not/ necessarily the end element of the "outermost" element!). So with that in mind it is important to know when to expect any end element, and "skip" these and "consume" the text values <in>between</in>.
So again thanks a lot for clarifying this!
Cheers, Oliver
--
Oliver Knoll
Dipl. Informatik-Ing. ETH
COMIT AG - ++41 79 520 95 22
More information about the Qt-interest-old
mailing list