CopperSpice API
1.9.2
|
The QXmlStreamReader class provides a fast parser for reading well formed XML via a simple streaming API. More...
Public Types | |
enum | Error |
enum | ReadElementTextBehaviour |
enum | TokenType |
The QXmlStreamReader class provides a fast parser for reading well formed XML via a simple streaming API. This class is faster than using the CopperSpice SAX parser. In some cases it might also be a better choice than using a DOM tree parser. QXmlStreamReader reads data either from a QIODevice or from a QByteArray. Refer to addData() for more information.
Refer to QXmlStreamWriter for writing an XML document.
The basic purppose of a stream reader is to parse 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 callback methods which receive XML events from the parser.
With QXmlStreamReader, your application controls the loop and retrieves tokens from the reader as required. This is done by calling readNext() which will read from the input stream until it reaches the next token and returns the tokenType(). A set of methods like isStartElement() and text() can be used to examine the token. The advantage of this application driven approach is the ability to build recursive descent parsers.
A typical loop with QXmlStreamReader might look like the following example.
QXmlStreamReader is a parser for well formed XML 1.0 without external parsed entities. As long as no error occurs the application code can be assured 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 occurs while parsing then atEnd() and hasError() both return true. The method error() will return the error which occurred. The methods errorString(), lineNumber(), columnNumber(), and characterOffset() are for constructing an appropriate error or warning message. To simplify application code, QXmlStreamReader contains a raiseError() mechanism that lets you raise custom errors that trigger the same error handling described.
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 does not 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.
QXmlStreamReader is an incremental parser. It can handle the case where the document can not be parsed all at once because it arrives in chunks (e.g. from multiple files, or over a network connection). When the reader runs out of data before the complete document has been parsed, it reports a PrematureEndOfDocumentError. When more data arrives, either because of a call to addData() or because more data is available through the network device(), the reader recovers from the PrematureEndOfDocumentError error and continues parsing the new data with the next call to readNext().
For example, if an application reads data from the network using a network access manager it would issue a network request to the manager and receive a network reply in return. Since a QNetworkReply inhertis from QIODevice, you can connect its readyRead() signal to a custom slot. In the slot read all available data with readAll() and pass it to the XML stream reader using addData(). Then call your custom parsing function to read the XML events from the reader.
QXmlStreamReader is conservative by design since it does not store the entire XML document tree in memory but instead stores the current token. This avoids small string allocations that it would normally take to map an XML document. Internally this class leverages QStringView for optimization.
This enum specifies different error cases
Constant | Value | Description |
---|---|---|
QXmlStreamReader::NoError | 0 | No error has occurred. |
QXmlStreamReader::CustomError | 2 | A custom error has been raised with raiseError() |
QXmlStreamReader::NotWellFormedError | 3 | The parser internally raised an error due to the read XML not being well-formed. |
QXmlStreamReader::PrematureEndOfDocumentError | 4 | The input stream ended before a well-formed XML document was parsed. Recovery from this error is possible if more XML arrives in the stream, either by calling addData() or by waiting for it to arrive on the device(). |
QXmlStreamReader::UnexpectedElementError | 1 | The parser encountered an element that was different to those it expected. |
This enum specifies the different behaviors of readElementText().
Constant | Value | Description |
---|---|---|
QXmlStreamReader::ErrorOnUnexpectedElement | 0 | Raise an UnexpectedElementError and return what was read so far when a child element is encountered. |
QXmlStreamReader::IncludeChildElements | 1 | Recursively include the text from child elements. |
QXmlStreamReader::SkipChildElements | 2 | Skip child elements. |
This enum specifies the type of token the reader just read.
Constant | Value | Description |
---|---|---|
QXmlStreamReader::NoToken | 0 | The reader has not yet read anything. |
QXmlStreamReader::Invalid | 1 | An error has occurred, reported in error() and errorString(). |
QXmlStreamReader::StartDocument | 2 | The reader reports the XML version number in documentVersion(), and the encoding as specified in the XML document in documentEncoding(). If the document is declared standalone, isStandaloneDocument() returns true, otherwise it returns false. |
QXmlStreamReader::EndDocument | 3 | The reader reports the end of the document. |
QXmlStreamReader::StartElement | 4 | The reader reports the start of an element with namespaceUri() and name(). Empty elements are also reported as StartElement, followed directly by EndElement. The method readElementText() can be called to concatenate all content until the corresponding EndElement. Attributes are reported in attributes(), namespace declarations in namespaceDeclarations(). |
QXmlStreamReader::EndElement | 5 | The reader reports the end of an element with namespaceUri() and name(). |
QXmlStreamReader::Characters | 6 | The reader reports characters in text(). If the characters are all white-space, isWhitespace() returns true. If the characters stem from a CDATA section, isCDATA() returns true. |
QXmlStreamReader::Comment | 7 | The reader reports a comment in text(). |
QXmlStreamReader::DTD | 8 | The reader reports a DTD in text(), notation declarations in notationDeclarations(), and entity declarations in entityDeclarations(). Details of the DTD declaration are reported in dtdName(), dtdPublicId(), and dtdSystemId(). |
QXmlStreamReader::EntityReference | 9 | The reader reports an entity reference that could not be resolved. The name of the reference is reported in name(), the replacement text in text(). |
QXmlStreamReader::ProcessingInstruction | 10 | The reader reports a processing instruction in processingInstructionTarget() and processingInstructionData(). |
QXmlStreamReader::QXmlStreamReader | ( | ) |
Constructs a stream reader.
QXmlStreamReader::QXmlStreamReader | ( | QIODevice * | device | ) |
Creates a new stream reader that reads from device.
QXmlStreamReader::QXmlStreamReader | ( | const QByteArray & | data | ) |
Creates a new stream reader that reads from data.
QXmlStreamReader::QXmlStreamReader | ( | const QString & | data | ) |
Creates a new stream reader that reads from data.
QXmlStreamReader::QXmlStreamReader | ( | const char * | data | ) |
Creates a new stream reader that reads from data.
QXmlStreamReader::~QXmlStreamReader | ( | ) |
Destructs the reader.
void QXmlStreamReader::addData | ( | const char * | data | ) |
Adds more data for the reader to read. This function does nothing if the reader has a device().
void QXmlStreamReader::addData | ( | const QByteArray & | data | ) |
Adds more data for the reader to read. This function does nothing if the reader has a device().
void QXmlStreamReader::addData | ( | const QString & | data | ) |
Adds more data for the reader to read. This function does nothing if the reader has a device().
void QXmlStreamReader::addExtraNamespaceDeclaration | ( | const QXmlStreamNamespaceDeclaration & | declaration | ) |
Adds a declaration. The declaration will be valid for children of the current element, If this method is called before any elements are read then the declaration is applied to the entire XML document.
void QXmlStreamReader::addExtraNamespaceDeclarations | ( | const QXmlStreamNamespaceDeclarations & | declarations | ) |
Adds a vector of declarations specified by declarations.
bool QXmlStreamReader::atEnd | ( | ) | const |
Returns true if the reader has read until the end of the XML document, or if an error() has occurred and reading has been aborted. Otherwise, it returns false.
When atEnd() and hasError() return true and error() returns PrematureEndOfDocumentError, it means the XML has been well-formed so far, but a complete XML document has not been parsed. The next chunk of XML can be added with addData(), if the XML is being read from a QByteArray, or by waiting for more data to arrive if the XML is being read from a QIODevice. Either way, atEnd() will return false once more data is available.
QXmlStreamAttributes QXmlStreamReader::attributes | ( | ) | const |
Returns the attributes of a StartElement.
qint64 QXmlStreamReader::characterOffset | ( | ) | const |
Returns the current character offset, starting with 0.
void QXmlStreamReader::clear | ( | ) |
qint64 QXmlStreamReader::columnNumber | ( | ) | const |
Returns the current column number, starting with 0.
QIODevice * QXmlStreamReader::device | ( | ) | const |
Returns the current device associated with the QXmlStreamReader or a nullptr if no device has been assigned.
QStringView QXmlStreamReader::documentEncoding | ( | ) | const |
If the state() is StartDocument, this function returns the encoding string as specified in the XML declaration. Otherwise an empty string is returned.
QStringView QXmlStreamReader::documentVersion | ( | ) | const |
If the state() is StartDocument, this function returns the version string as specified in the XML declaration. Otherwise an empty string is returned.
QStringView QXmlStreamReader::dtdName | ( | ) | const |
If the state() is DTD, this function returns the DTD's name. Otherwise an empty string is returned.
QStringView QXmlStreamReader::dtdPublicId | ( | ) | const |
If the state() is DTD, this function returns the DTD's public identifier. Otherwise an empty string is returned.
QStringView QXmlStreamReader::dtdSystemId | ( | ) | const |
If the state() is DTD, this function returns the DTD's system identifier. Otherwise an empty string is returned.
QXmlStreamEntityDeclarations QXmlStreamReader::entityDeclarations | ( | ) | const |
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.
QXmlStreamEntityResolver * QXmlStreamReader::entityResolver | ( | ) | const |
Returns the entity resolver or a nullptr if there is no entity resolver.
Error QXmlStreamReader::error | ( | ) | const |
Returns the type of the current error, or NoError if no error occurred.
QString QXmlStreamReader::errorString | ( | ) | const |
Returns the error message that was set with raiseError().
|
inline |
Returns true
if an error has occurred, otherwise false
.
bool QXmlStreamReader::isCDATA | ( | ) | const |
Returns true if the reader reports characters that stem from a CDATA section, otherwise returns false.
|
inline |
Returns true if tokenType() equals Characters, otherwise returns false.
|
inline |
Returns true if tokenType() equals Comment, otherwise returns false.
|
inline |
Returns true if tokenType() equals DTD, otherwise returns false.
|
inline |
Returns true if tokenType() equals EndDocument, otherwise returns false.
|
inline |
Returns true if tokenType() equals EndElement, otherwise returns false.
|
inline |
Returns true if tokenType() equals EntityReference, otherwise returns false.
|
inline |
Returns true if tokenType() equals ProcessingInstruction, otherwise returns false.
bool QXmlStreamReader::isStandaloneDocument | ( | ) | const |
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.
|
inline |
Returns true if tokenType() equals StartDocument, otherwise returns false.
|
inline |
Returns true if tokenType() equals StartElement, otherwise returns false.
bool QXmlStreamReader::isWhitespace | ( | ) | const |
Returns true if the reader reports characters that only consist of white-space, otherwise returns false.
qint64 QXmlStreamReader::lineNumber | ( | ) | const |
Returns the current line number, starting with 1.
QStringView QXmlStreamReader::name | ( | ) | const |
Returns the local name of a StartElement, EndElement, or an EntityReference.
QXmlStreamNamespaceDeclarations QXmlStreamReader::namespaceDeclarations | ( | ) | const |
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.
bool QXmlStreamReader::namespaceProcessing | ( | ) | const |
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.
QStringView QXmlStreamReader::namespaceUri | ( | ) | const |
Returns the namespaceUri of a StartElement or EndElement.
QXmlStreamNotationDeclarations QXmlStreamReader::notationDeclarations | ( | ) | const |
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.
QStringView QXmlStreamReader::prefix | ( | ) | const |
Returns the prefix of a StartElement or EndElement.
QStringView QXmlStreamReader::processingInstructionData | ( | ) | const |
Returns the data of a ProcessingInstruction.
QStringView QXmlStreamReader::processingInstructionTarget | ( | ) | const |
Returns the target of a ProcessingInstruction.
QStringView QXmlStreamReader::qualifiedName | ( | ) | const |
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 should not use qualifiedName(), but the resolved namespaceUri() and the attribute's local name().
Raises a custom error with an optional error message.
QString QXmlStreamReader::readElementText | ( | ) |
Calling this function is equivalent to calling readElementText(ErrorOnUnexpectedElement).
QString QXmlStreamReader::readElementText | ( | ReadElementTextBehaviour | behavior | ) |
Called in case a StartElement was read. Reads until the corresponding EndElement and returns all text in-between. In case of no error, the current token (see tokenType()) after having called this function is EndElement. The method concatenates text() when it reads either Characters or EntityReference tokens, but skips ProcessingInstruction and Comment. If the current token is not StartElement, an empty string is returned.
The behavior defines what happens in case anything else is read before reaching EndElement. This method can include the text from child elements (useful for example for HTML), ignore child elements, or raise an UnexpectedElementError and return what was read so far.
TokenType QXmlStreamReader::readNext | ( | ) |
Reads the next token and returns its type.
With one exception, once an error() is reported by readNext(), further reading of the XML stream is not possible. Then atEnd() returns true, hasError() returns true, and this function returns QXmlStreamReader::Invalid.
The exception is when error() returns PrematureEndOfDocumentError. This error is reported when the end of an otherwise well-formed chunk of XML is reached, but the chunk does not represent a complete XML document. In that case, parsing can be resumed by calling addData() to add the next chunk of XML, when the stream is being read from a QByteArray, or by waiting for more data to arrive when the stream is being read from a device().
bool QXmlStreamReader::readNextStartElement | ( | ) |
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.
You can traverse a document by repeatedly calling this function while ensuring that the stream reader is not at the end of the document:
This method should be used when you are only concerned with parsing XML elements.
void QXmlStreamReader::setDevice | ( | QIODevice * | device | ) |
void QXmlStreamReader::setEntityResolver | ( | QXmlStreamEntityResolver * | resolver | ) |
Makes resolver the new entityResolver().
The stream reader does not take ownership of the resolver. It is the callers responsibility to ensure the resolver is valid during the entire life time of the stream reader object, or until another resolver or nullpter is set.
void QXmlStreamReader::setNamespaceProcessing | ( | bool | enable | ) |
Sets the value of the property to enable.
void QXmlStreamReader::skipCurrentElement | ( | ) |
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.
QStringView QXmlStreamReader::text | ( | ) | const |
Returns the text of Characters, Comment, DTD, or EntityReference.
QString QXmlStreamReader::tokenString | ( | ) | const |
Returns the reader's current token as string.
TokenType QXmlStreamReader::tokenType | ( | ) | const |
Returns the type of the current token. The current token can also be queried by calling isStartDocument(), isEndDocument(), isStartElement(), isEndElement(), isCharacters(), isComment(), isDTD(), isEntityReference(), or isProcessingInstruction().