diff options
author | Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com> | 2009-08-17 14:55:58 (GMT) |
---|---|---|
committer | Thorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com> | 2009-08-18 17:10:56 (GMT) |
commit | 393f5d5b2705c0ed7e6e1a3a69cc9cdf16cf334d (patch) | |
tree | 3b20e5280bf629e6360eaa35b23019e77b66a4c4 /src | |
parent | e08ca6bcaf5fc746fdf8f3e379c17bf0a9daa771 (diff) | |
download | Qt-393f5d5b2705c0ed7e6e1a3a69cc9cdf16cf334d.zip Qt-393f5d5b2705c0ed7e6e1a3a69cc9cdf16cf334d.tar.gz Qt-393f5d5b2705c0ed7e6e1a3a69cc9cdf16cf334d.tar.bz2 |
Added two convenience functions to QXmlStreamReader
QXmlStreamReader::readNextStartElement reads until the next start
element within the current element, or returns false when no such
element is encountered before the end element is reached. It simplifies
the common case of iterating over the elements in an XML document.
QXmlStreamReader::skipCurrentElement reads until the end element of the
current element, skipping any child elements. This functionality was
requested in two tasks, and a similar function 'readUnknownElement' was
present in Qt's stream reader example.
Autotest is included, example and documentation have been updated.
Task-number: 238793
Reviewed-by: mae
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/xml/qxmlstream.cpp | 49 | ||||
-rw-r--r-- | src/corelib/xml/qxmlstream.h | 3 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/corelib/xml/qxmlstream.cpp b/src/corelib/xml/qxmlstream.cpp index a08b167..8b2462e 100644 --- a/src/corelib/xml/qxmlstream.cpp +++ b/src/corelib/xml/qxmlstream.cpp @@ -622,6 +622,55 @@ QXmlStreamReader::TokenType QXmlStreamReader::tokenType() const return d->type; } +/*! + Reads until the next start element within the current element. Returns true + when a start element was reached. When the end element was reached, or when + an error occurred, false is returned. + + The current element is the element matching the most recently parsed start + element of which a matching end element has not yet been reached. When the + parser has reached the end element, the current element becomes the parent + element. + + This is a convenience function for when you're only concerned with parsing + XML elements. The \l{QXmlStream Bookmarks Example} makes extensive use of + this function. + + \since 4.6 + */ +bool QXmlStreamReader::readNextStartElement() +{ + while (readNext() != Invalid) { + if (isEndElement()) + return false; + else if (isStartElement()) + return true; + } + return false; +} + +/*! + Reads until the end of the current element, skipping any child nodes. + This function is useful for skipping unknown elements. + + The current element is the element matching the most recently parsed start + element of which a matching end element has not yet been reached. When the + parser has reached the end element, the current element becomes the parent + element. + + \since 4.6 + */ +void QXmlStreamReader::skipCurrentElement() +{ + int depth = 1; + while (depth && readNext() != Invalid) { + if (isEndElement()) + --depth; + else if (isStartElement()) + ++depth; + } +} + /* * Use the following Perl script to generate the error string index list: ===== PERL SCRIPT ==== diff --git a/src/corelib/xml/qxmlstream.h b/src/corelib/xml/qxmlstream.h index 420a66a..21dcb40 100644 --- a/src/corelib/xml/qxmlstream.h +++ b/src/corelib/xml/qxmlstream.h @@ -321,6 +321,9 @@ public: bool atEnd() const; TokenType readNext(); + bool readNextStartElement(); + void skipCurrentElement(); + TokenType tokenType() const; QString tokenString() const; |