This XML reader is suitable for a wide range of applications. It is able to parse well-formed XML and can report the namespaces of elements to a content handler; however, it does not parse any external entities. For historical reasons, Attribute Value Normalization and End-of-Line Handling as described in the XML 1.0 specification is not performed.
The easiest pattern of use for this class is to create a reader instance, define an input source, specify the handlers to be used by the reader, and parse the data.
For example, we could use a
QFileto supply the input. Here, we create a reader, and define an input source to be used by the reader:QXmlSimpleReader xmlReader; QXmlInputSource *source = new QXmlInputSource(file);
A handler lets us perform actions when the reader encounters certain types of content, or if errors in the input are found. The reader must be told which handler to use for each type of event. For many common applications, we can create a custom handler by subclassing
QXmlDefaultHandler, and use this to handle both error and content events:Handler *handler = new Handler; xmlReader.setContentHandler(handler); xmlReader.setErrorHandler(handler);
If you don’t set at least the content and error handlers, the parser will fall back on its default behavior—and will do nothing.
The most convenient way to handle the input is to read it in a single pass using the
parse()function with an argument that specifies the input source:bool ok = xmlReader.parse(source); if (!ok) std::cout << "Parsing failed." << std::endl;
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. This is achieved by telling
parse()to work incrementally, and making subsequent calls to the
parseContinue()function, until all the data has been processed.
A common way to perform incremental parsing is to connect the
readyRead()signal of a
network replya slot, and handle the incoming data there. See
Aspects of the parsing behavior can be adapted using
Note that this class is now deprecated, please use
QDomDocumentfor reading XML files.
- class PySide2.QtXml.QXmlSimpleReader¶
Constructs a simple XML reader.
- PySide2.QtXml.QXmlSimpleReader.parse(input, incremental)¶
incremental – bool
- Return type:
Reads an XML document from
inputand parses it. Returns
trueif the parsing is completed successfully; otherwise returns
false, indicating that an error occurred.
incrementalis false, this function will return false if the XML file is not read completely. The parsing cannot be continued in this case.
incrementalis true, the parser does not return false if it reaches the end of the
inputbefore reaching the end of the XML file. Instead, it stores the state of the parser so that parsing can be continued later when more data is available. In such a case, you can use the function
parseContinue()to continue with parsing. This class stores a pointer to the input source
parseContinue()function tries to read from that input source. Therefore, you should not delete the input source
inputuntil you no longer need to call
If this function is called with
incrementalset to true while an incremental parse is in progress, a new parsing session will be started, and the previous session will be lost.
- Return type:
Continues incremental parsing, taking input from the
QXmlInputSourcethat was specified with the most recent call to
parse(). To use this function, you must have called
parse()with the incremental argument set to true.
falseif a parsing error occurs; otherwise returns
true, even if the end of the XML file has not been reached. You can continue parsing at a later stage by calling this function again when there is more data available to parse.
Calling this function when there is no data available in the input source indicates to the reader that the end of the XML file has been reached. If the input supplied up to this point was not well-formed then a parsing error occurs, and false is returned. If the input supplied was well-formed, true is returned. It is important to end the input in this way because it allows you to reuse the reader to parse other XML files.
Calling this function after the end of file has been reached, but without available data will cause false to be returned whether the previous input was well-formed or not.
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.