Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.trolltech.qt.xml
Class QXmlStreamReader

    java.lang.Object
      com.trolltech.qt.internal.QSignalEmitterInternal
          com.trolltech.qt.QSignalEmitter
              com.trolltech.qt.QtJambiObject
                  com.trolltech.qt.xml.QXmlStreamReader

All Implemented Interfaces:
  QtJambiInterface

    public class QXmlStreamReader

extends QtJambiObject

The `QXmlStreamReader` class provides a fast well-formed XML parser with a simple streaming API. `QXmlStreamReader` is a faster and more convenient replacement for Qt's own SAX parser (see `QXmlSimpleReader`), and in some cases also for applications that would previously use a DOM tree (see `QDomDocument`). `QXmlStreamReader` reads data either from a `QIODevice` (see `setDevice()`), or from a raw `QByteArray` (see `addData()`). With `QXmlStreamWriter`, Qt provides a related class for writing XML.

The basic concept of a stream reader is to report an XML document as a stream of tokens, similar to SAX. The main difference between `QXmlStreamReader` and SAX is how these XML tokens are reported. With SAX, the application must provide handlers that receive so-called XML events from the parser at the parser's convenience. With `QXmlStreamReader`, the application code itself drives the loop and pulls tokens from the reader one after another as it needs them. This is done by calling `readNext()`, which makes the reader read from the input stream until it has completed a new token, and then returns its `tokenType()`. A set of convenient functions like `isStartElement()` or `text()` then allows to examine this token, and to obtain information about what has been read. The big advantage of the pulling approach is the possibility to build recursive descent parsers, meaning you can split your XML parsing code easily into different methods or classes. This makes it easy to keep track of the application's own state when parsing XML.

A typical loop with `QXmlStreamReader` looks like this:

            QXmlStreamReader xml = new QXmlStreamReader();
            //...
            while (!xml.atEnd()) {
                xml.readNext();
                // do processing ...
            }
            if (xml.hasError()) {
                // do error handling ...
            }

`QXmlStreamReader` is a well-formed XML 1.0 parser that does not include external parsed entities. As long as no error occurs, the application code can thus be assured that the data provided by the stream reader satisfies the W3C's criteria for well-formed XML. For example, you can be certain that all tags are indeed nested and closed properly, that references to internal entities have been replaced with the correct replacement text, and that attributes have been normalized or added according to the internal subset of the DTD.

If an error does occur while parsing, `atEnd()` returns true and `error()` returns the kind of error that occurred. `hasError()` can also be used to check whether an error has occurred. The functions `errorString()`, `lineNumber()`, `columnNumber()`, and `characterOffset()` make it possible to generate a verbose human-understandable error or warning message. In order to simplify application code, `QXmlStreamReader` contains a `raiseError()` mechanism that makes it possible to raise custom errors that then trigger the same error handling code path.

The QXmlStream Bookmarks Example illustrates how to use the recursive descent technique with a subclassed stream reader to read an XML bookmark file (XBEL).

## Namespaces

QXmlStream understands and resolves XML namespaces. E.g. in case of a `StartElement` , `namespaceUri()` returns the namespace the element is in, and `name()` returns the element's local name. The combination of `namespaceUri` and name uniquely identifies an element. If a namespace prefix was not declared in the XML entities parsed by the reader, the `namespaceUri` is empty.

If you parse XML data that does not utilize namespaces according to the XML specification or doesn't use namespaces at all, you can use the element's `qualifiedName()` instead. A qualified name is the element's `prefix()` followed by colon followed by the element's local `name()` - exactly like the element appears in the raw XML data. Since the mapping `namespaceUri` to prefix is neither unique nor universal, `qualifiedName()` should be avoided for namespace-compliant XML data.

In order to parse standalone documents that do use undeclared namespace prefixes, you can turn off namespace processing completely with the `namespaceProcessing` property.

## Incremental parsing

`QXmlStreamReader` is an incremental parser. If you can't parse the entire input in one go (for example, it is huge, or is being delivered over a network connection), data can be fed to the parser in pieces. If the reader runs out of data before the document has been parsed completely, it reports a `PrematureEndOfDocumentError` . Once more data has arrived, either through the device or because it has been added with `addData()`, it recovers from that error and continues parsing on the next call to `read()`.

For example, if you read data from the network using `QHttp`, you would connect its `readyRead()` signal to a custom slot. In this slot, you read all available data with `readAll()` and pass it to the XML stream reader using `addData()`. Then you call your custom parsing function that reads the XML events from the reader.

## Performance and memory consumption

`QXmlStreamReader` is memory-conservative by design, since it doesn't store the entire XML document tree in memory, but only the current token at the time it is reported. In addition, `QXmlStreamReader` avoids the many small string allocations that it normally takes to map an XML document to a convenient and Qt-ish API. It does this by reporting all string data as QStringRef rather than real QString objects. QStringRef is a thin wrapper around QString substrings that provides a subset of the QString API without the memory allocation and reference-counting overhead. Calling toString() on any of those objects returns an equivalent real QString object.

Nested Class Summary
`static class` | `QXmlStreamReader.Error` This enum specifies different error cases
`static class` | `QXmlStreamReader.TokenType` This enum specifies the type of token the reader just read.

Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
`QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9`

Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal`

Field Summary

Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`currentSender`

Constructor Summary
`QXmlStreamReader()` Constructs a stream reader.
`QXmlStreamReader(QByteArray data)` Creates a new stream reader that reads from `data`.
`QXmlStreamReader(QIODevice device)` Creates a new stream reader that reads from `device`.
`QXmlStreamReader(java.lang.String data)` Creates a new stream reader that reads from `data`.

Method Summary
`void` | `addData(QByteArray data)` Adds more `data` for the reader to read.
`void` | `addData(java.lang.String data)` Adds more `data` for the reader to read.
`void` | `addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration extraNamespaceDeclaraction)` Adds an `extraNamespaceDeclaration`.
`void` | `addExtraNamespaceDeclarations(java.util.List extraNamespaceDeclaractions)` Function: void addExtraNamespaceDeclarations(java.util.List extraNamespaceDeclaractions)
`boolean` | `atEnd()` Returns true if the reader has read until the end of the XML document, or an error has occurred and reading has been aborted; otherwise returns false.
`QXmlStreamAttributes` | `attributes()` Returns the attributes of a `StartElement` .
`long` | `characterOffset()` Returns the current character offset, starting with 0.
`void` | `clear()` Removes any `device()` or data from the reader, and resets its state to the initial state.
`long` | `columnNumber()` Returns the current column number, starting with 0.
`QIODevice` | `device()` Returns the current device associated with the `QXmlStreamReader`, or 0 if no device has been assigned.
`java.lang.String` | `documentEncoding()` If the state() is `StartDocument` , this function returns the encoding string as specified in the XML declaration.
`java.lang.String` | `documentVersion()` If the state() is `StartDocument` , this function returns the version string as specified in the XML declaration.
`java.lang.String` | `dtdName()` If the state() is `DTD` , this function returns the DTD's name.
`java.lang.String` | `dtdPublicId()` If the state() is `DTD` , this function returns the DTD's public identifier.
`java.lang.String` | `dtdSystemId()` If the state() is `DTD` , this function returns the DTD's system identifier.
`java.util.List` | `entityDeclarations()` If the state() is `DTD` , this function returns the DTD's unparsed (external) entity declarations.
`QXmlStreamEntityResolver` | `entityResolver()` Returns the entity resolver, or 0 if there is no entity resolver.
`QXmlStreamReader.Error` | `error()` Returns the type of the current error, or `NoError` if no error occurred.
`java.lang.String` | `errorString()` Returns the error message that was set with `raiseError()`.
`boolean` | `hasError()` Returns `true` if an error has occurred, otherwise `false`.
`boolean` | `isCDATA()` Returns true if the reader reports characters that stem from a CDATA section; otherwise returns false.
`boolean` | `isCharacters()` Returns true if `tokenType()` equals `Characters` ; otherwise returns false.
`boolean` | `isComment()` Returns true if `tokenType()` equals `Comment` ; otherwise returns false.
`boolean` | `isDTD()` Returns true if `tokenType()` equals `DTD` ; otherwise returns false.
`boolean` | `isEndDocument()` Returns true if `tokenType()` equals `EndDocument` ; otherwise returns false.
`boolean` | `isEndElement()` Returns true if `tokenType()` equals `EndElement` ; otherwise returns false.
`boolean` | `isEntityReference()` Returns true if `tokenType()` equals `EntityReference` ; otherwise returns false.
`boolean` | `isProcessingInstruction()` Returns true if `tokenType()` equals `ProcessingInstruction` ; otherwise returns false.
`boolean` | `isStandaloneDocument()` Returns true if this document has been declared standalone in the XML declaration; otherwise returns false.
`boolean` | `isStartDocument()` Returns true if `tokenType()` equals `StartDocument` ; otherwise returns false.
`boolean` | `isStartElement()` Returns true if `tokenType()` equals `StartElement` ; otherwise returns false.
`boolean` | `isWhitespace()` Returns true if the reader reports characters that only consist of white-space; otherwise returns false.
`long` | `lineNumber()` Returns the current line number, starting with 1.
`java.lang.String` | `name()` Returns the local name of a `StartElement` , `EndElement` , or an `EntityReference` .
`java.util.List` | `namespaceDeclarations()` If the state() is `StartElement` , this function returns the element's namespace declarations.
`boolean` | `namespaceProcessing()` the namespace-processing flag of the stream reader
`java.lang.String` | `namespaceUri()` Returns the `namespaceUri` of a `StartElement` or `EndElement` .
`java.util.List` | `notationDeclarations()` If the state() is `DTD` , this function returns the DTD's notation declarations.
`java.lang.String` | `prefix()` Returns the prefix of a `StartElement` or `EndElement` .
`java.lang.String` | `processingInstructionData()` Returns the data of a `ProcessingInstruction` .
`java.lang.String` | `processingInstructionTarget()` Returns the target of a `ProcessingInstruction` .
`java.lang.String` | `qualifiedName()` Returns the qualified name of a `StartElement` or `EndElement` ;
`void` | `raiseError()` Raises a custom error with an optional error `message`.
`void` | `raiseError(java.lang.String message)` Raises a custom error with an optional error `message`.
`java.lang.String` | `readElementText()` Convenience function to be called in case a `StartElement` was read.
`QXmlStreamReader.TokenType` | `readNext()` Reads the next token and returns its type.
`void` | `setDevice(QIODevice device)` Sets the current device to `device`.
`void` | `setEntityResolver(QXmlStreamEntityResolver resolver)` Makes `resolver` the new `entityResolver()`.
`void` | `setNamespaceProcessing(boolean arg__1)` the namespace-processing flag of the stream reader
`java.lang.String` | `text()` Returns the text of `Characters` , `Comment` , `DTD` , or `EntityReference` .
`java.lang.String` | `tokenString()` Returns the reader's current token as string.
`QXmlStreamReader.TokenType` | `tokenType()` Returns the type of the current token.

Methods inherited from class com.trolltech.qt.QtJambiObject
`dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr`

Methods inherited from class com.trolltech.qt.QSignalEmitter
`blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread`

Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal
`__qt_signalInitialization`

Methods inherited from class java.lang.Object
`clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Methods inherited from interface com.trolltech.qt.QtJambiInterface
`disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership`

Constructor Detail

### QXmlStreamReader

    public QXmlStreamReader()

  Constructs a stream reader.

See also:
  `setDevice()`, and `addData()`.

### QXmlStreamReader

    public QXmlStreamReader(QIODevice device)

  Creates a new stream reader that reads from `device`.

See also:
  `setDevice()`, and `clear()`.

### QXmlStreamReader

    public QXmlStreamReader(QByteArray data)

  Creates a new stream reader that reads from `data`.

See also:
  `addData()`, `clear()`, and `setDevice()`.

### QXmlStreamReader

    public QXmlStreamReader(java.lang.String data)

  Creates a new stream reader that reads from `data`.

See also:
  `addData()`, `clear()`, and `setDevice()`.

Method Detail

### addData

    public final void addData(QByteArray data)

  Adds more `data` for the reader to read.

  This function does nothing if the reader has a `device()`.

See also:
  `clear()`.

### addData

    public final void addData(java.lang.String data)

  Adds more `data` for the reader to read.

  This function does nothing if the reader has a `device()`.

See also:
  `clear()`.

### addExtraNamespaceDeclaration

    public final void addExtraNamespaceDeclaration(QXmlStreamNamespaceDeclaration extraNamespaceDeclaraction)

  Adds an `extraNamespaceDeclaration`. The declaration will be valid for children of the current element, or - should the function be called before any elements are read - for the entire XML document.

See also:
  `namespaceDeclarations()`, `addExtraNamespaceDeclarations()`, and `setNamespaceProcessing()`.

### addExtraNamespaceDeclarations

    public final void addExtraNamespaceDeclarations(java.util.List extraNamespaceDeclaractions)

  Function: void addExtraNamespaceDeclarations(java.util.List extraNamespaceDeclaractions)

### atEnd

    public final boolean atEnd()

  Returns true if the reader has read until the end of the XML document, or an error has occurred and reading has been aborted; otherwise returns false.

  Has reading been aborted with a `PrematureEndOfDocumentError` because the device no longer delivered data, `atEnd()` will return true once more data has arrived.

See also:
  `device()`, and `QIODevice::atEnd()`.

### attributes

    public final QXmlStreamAttributes attributes()

  Returns the attributes of a `StartElement` .

### characterOffset

    public final long characterOffset()

  Returns the current character offset, starting with 0.

See also:
  `lineNumber()`, and `columnNumber()`.

### clear

    public final void clear()

  Removes any `device()` or data from the reader, and resets its state to the initial state.

See also:
  `addData()`.

### columnNumber

    public final long columnNumber()

  Returns the current column number, starting with 0.

See also:
  `lineNumber()`, and `characterOffset()`.

### device

    public final QIODevice device()

  Returns the current device associated with the `QXmlStreamReader`, or 0 if no device has been assigned.

See also:
  `setDevice()`.

### documentEncoding

    public final java.lang.String documentEncoding()

  If the state() is `StartDocument` , this function returns the encoding string as specified in the XML declaration. Otherwise an empty string is returned.

### documentVersion

    public final java.lang.String documentVersion()

  If the state() is `StartDocument` , this function returns the version string as specified in the XML declaration. Otherwise an empty string is returned.

### dtdName

    public final java.lang.String dtdName()

  If the state() is `DTD` , this function returns the DTD's name. Otherwise an empty string is returned.

### dtdPublicId

    public final java.lang.String dtdPublicId()

  If the state() is `DTD` , this function returns the DTD's public identifier. Otherwise an empty string is returned.

### dtdSystemId

    public final java.lang.String dtdSystemId()

  If the state() is `DTD` , this function returns the DTD's system identifier. Otherwise an empty string is returned.

### entityDeclarations

    public final java.util.List entityDeclarations()

  If the state() is `DTD` , this function returns the DTD's unparsed (external) entity declarations. Otherwise an empty vector is returned.

  The QXmlStreamEntityDeclarations class is defined to be a QVector of `QXmlStreamEntityDeclaration`.

### entityResolver

    public final QXmlStreamEntityResolver entityResolver()

  Returns the entity resolver, or 0 if there is no entity resolver.

See also:
  `setEntityResolver()`.

### error

    public final QXmlStreamReader.Error error()

  Returns the type of the current error, or `NoError` if no error occurred.

See also:
  `errorString()`, and `raiseError()`.

### errorString

    public final java.lang.String errorString()

  Returns the error message that was set with `raiseError()`.

See also:
  `error()`, `lineNumber()`, `columnNumber()`, and `characterOffset()`.

### hasError

    public final boolean hasError()

  Returns `true` if an error has occurred, otherwise `false`.

See also:
  `errorString()`, and `error()`.

### isCDATA

    public final boolean isCDATA()

  Returns true if the reader reports characters that stem from a CDATA section; otherwise returns false.

See also:
  `isCharacters()`, and `text()`.

### isCharacters

    public final boolean isCharacters()

  Returns true if `tokenType()` equals `Characters` ; otherwise returns false.

See also:
  `isWhitespace()`, and `isCDATA()`.

### isComment

    public final boolean isComment()

  Returns true if `tokenType()` equals `Comment` ; otherwise returns false.

### isDTD

    public final boolean isDTD()

  Returns true if `tokenType()` equals `DTD` ; otherwise returns false.

### isEndDocument

    public final boolean isEndDocument()

  Returns true if `tokenType()` equals `EndDocument` ; otherwise returns false.

### isEndElement

    public final boolean isEndElement()

  Returns true if `tokenType()` equals `EndElement` ; otherwise returns false.

### isEntityReference

    public final boolean isEntityReference()

  Returns true if `tokenType()` equals `EntityReference` ; otherwise returns false.

### isProcessingInstruction

    public final boolean isProcessingInstruction()

  Returns true if `tokenType()` equals `ProcessingInstruction` ; otherwise returns false.

### isStandaloneDocument

    public final boolean isStandaloneDocument()

  Returns true if this document has been declared standalone in the XML declaration; otherwise returns false.

  If no XML declaration has been parsed, this function returns false.

### isStartDocument

    public final boolean isStartDocument()

  Returns true if `tokenType()` equals `StartDocument` ; otherwise returns false.

### isStartElement

    public final boolean isStartElement()

  Returns true if `tokenType()` equals `StartElement` ; otherwise returns false.

### isWhitespace

    public final boolean isWhitespace()

  Returns true if the reader reports characters that only consist of white-space; otherwise returns false.

See also:
  `isCharacters()`, and `text()`.

### lineNumber

    public final long lineNumber()

  Returns the current line number, starting with 1.

See also:
  `columnNumber()`, and `characterOffset()`.

### name

    public final java.lang.String name()

  Returns the local name of a `StartElement` , `EndElement` , or an `EntityReference` .

See also:
  `namespaceUri()`, and `qualifiedName()`.

### namespaceDeclarations

    public final java.util.List namespaceDeclarations()

  If the state() is `StartElement` , this function returns the element's namespace declarations. Otherwise an empty vector is returned.

  The `QXmlStreamNamespaceDeclaration` class is defined to be a QVector of `QXmlStreamNamespaceDeclaration`.

See also:
  `addExtraNamespaceDeclaration()`, and `addExtraNamespaceDeclarations()`.

### namespaceProcessing

    public final boolean namespaceProcessing()

  the namespace-processing flag of the stream reader

  This property controls whether or not the stream reader processes namespaces. If enabled, the reader processes namespaces, otherwise it does not.

  By default, namespace-processing is enabled.

### namespaceUri

    public final java.lang.String namespaceUri()

  Returns the `namespaceUri` of a `StartElement` or `EndElement` .

See also:
  `name()`, and `qualifiedName()`.

### notationDeclarations

    public final java.util.List notationDeclarations()

  If the state() is `DTD` , this function returns the DTD's notation declarations. Otherwise an empty vector is returned.

  The QXmlStreamNotationDeclarations class is defined to be a QVector of `QXmlStreamNotationDeclaration`.

### prefix

    public final java.lang.String prefix()

  Returns the prefix of a `StartElement` or `EndElement` .

See also:
  `name()`, and `qualifiedName()`.

### processingInstructionData

    public final java.lang.String processingInstructionData()

  Returns the data of a `ProcessingInstruction` .

### processingInstructionTarget

    public final java.lang.String processingInstructionTarget()

  Returns the target of a `ProcessingInstruction` .

### qualifiedName

    public final java.lang.String qualifiedName()

  Returns the qualified name of a `StartElement` or `EndElement` ;

  A qualified name is the raw name of an element in the XML data. It consists of the namespace prefix, followed by colon, followed by the element's local name. Since the namespace prefix is not unique (the same prefix can point to different namespaces and different prefixes can point to the same namespace), you shouldn't use `qualifiedName()`, but the resolved `namespaceUri()` and the attribute's local `name()`.

See also:
  `name()`, `prefix()`, and `namespaceUri()`.

### raiseError

    public final void raiseError()

  Raises a custom error with an optional error `message`.

See also:
  `error()`, and `errorString()`.

### raiseError

    public final void raiseError(java.lang.String message)

  Raises a custom error with an optional error `message`.

See also:
  `error()`, and `errorString()`.

### readElementText

    public final java.lang.String readElementText()

  Convenience function to be called in case a `StartElement` was read. Reads until the corresponding `EndElement` and returns all text in-between. In case of no error, the token after having called this function is `EndElement` .

  The function concatenates `text()` when it reads either `Characters` or `EntityReference` tokens, but skips `ProcessingInstruction` and `Comment` . In case anything else is read before reaching `EndElement` , the function returns what it read so far and raises an `UnexpectedElementError` . If the current token is not `StartElement` , an empty string is returned.

### readNext

    public final QXmlStreamReader.TokenType readNext()

  Reads the next token and returns its type.

  If an `error()` has been reported, reading is no longer possible. In this case, `atEnd()` always returns true, and this function will do nothing but return Invalid.

  The one exception to this rule are errors of type `PrematureEndOfDocumentError` . Subsequent calls to `atEnd()` and `readNext()` will resume this error type and try to read from the device again. This iterative parsing approach makes sense if you can't or don't want to read the entire data in one go, for example, if it is huge, or it is being delivered over a network connection

See also:
  `tokenType()`, and `tokenString()`.

### setDevice

    public final void setDevice(QIODevice device)

  Sets the current device to `device`. Setting the device resets the stream to its initial state.

See also:
  `device()`, and `clear()`.

### setEntityResolver

    public final void setEntityResolver(QXmlStreamEntityResolver resolver)

  Makes `resolver` the new `entityResolver()`.

  The stream reader does not take ownership of the resolver. It's the callers responsibility to ensure that the resolver is valid during the entire life-time of the stream reader object, or until another resolver or 0 is set.

See also:
  `entityResolver()`.

### setNamespaceProcessing

    public final void setNamespaceProcessing(boolean arg__1)

  the namespace-processing flag of the stream reader

  This property controls whether or not the stream reader processes namespaces. If enabled, the reader processes namespaces, otherwise it does not.

  By default, namespace-processing is enabled.

### text

    public final java.lang.String text()

  Returns the text of `Characters` , `Comment` , `DTD` , or `EntityReference` .

### tokenString

    public final java.lang.String tokenString()

  Returns the reader's current token as string.

See also:
  `tokenType()`.

### tokenType

    public final QXmlStreamReader.TokenType tokenType()

  Returns the type of the current token.

  The current token can also be queried with the convenience functions `isStartDocument()`, `isEndDocument()`, `isStartElement()`, `isEndElement()`, `isCharacters()`, `isComment()`, `isDTD()`, `isEntityReference()`, and `isProcessingInstruction()`

See also:
  `tokenString()`.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD