QXmlSimpleReader 类提供简单 XML 剖析器的实现。 更多...
头: | #include <QXmlSimpleReader> |
CMake: |
find_package(Qt6 COMPONENTS Core5Compat REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core5Compat) |
qmake: | QT += core5compat |
继承: | QXmlReader |
警告: 此类不 可重入 .
QXmlSimpleReader () | |
virtual | ~QXmlSimpleReader () |
virtual bool | parse (const QXmlInputSource * input , bool incremental ) |
virtual bool | parseContinue () |
virtual QXmlDTDHandler * | DTDHandler () const override |
virtual QXmlContentHandler * | contentHandler () const override |
virtual QXmlDeclHandler * | declHandler () const override |
virtual QXmlEntityResolver * | entityResolver () const override |
virtual QXmlErrorHandler * | errorHandler () const override |
virtual bool | feature (const QString & name , bool * ok = nullptr) const override |
virtual bool | hasFeature (const QString & name ) const override |
virtual bool | hasProperty (const QString & name ) const override |
virtual QXmlLexicalHandler * | lexicalHandler () const override |
virtual bool | parse (const QXmlInputSource & input ) override |
virtual bool | parse (const QXmlInputSource * input ) override |
virtual void * | property (const QString & name , bool * ok = nullptr) const override |
virtual void | setContentHandler (QXmlContentHandler * handler ) override |
virtual void | setDTDHandler (QXmlDTDHandler * handler ) override |
virtual void | setDeclHandler (QXmlDeclHandler * handler ) override |
virtual void | setEntityResolver (QXmlEntityResolver * handler ) override |
virtual void | setErrorHandler (QXmlErrorHandler * handler ) override |
virtual void | setFeature (const QString & name , bool enable ) override |
virtual void | setLexicalHandler (QXmlLexicalHandler * handler ) override |
virtual void | setProperty (const QString & name , void * value ) override |
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 QFile to 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 reply
a slot, and handle the incoming data there. See
QNetworkAccessManager
.
Aspects of the parsing behavior can be adapted using setFeature () 和 setProperty ().
xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
QXmlSimpleReader is not reentrant. If you want to use the class in threaded code, lock the code using QXmlSimpleReader with a locking mechanism, such as a QMutex .
注意:此类现被弃用,请使用 QXmlStreamReader 或 QDomDocument 为读取 XML 文件。
构造简单 XML 读取器。
[虚拟]
QXmlSimpleReader::
~QXmlSimpleReader
()
销毁简单 XML 读取器。
[override virtual]
QXmlDTDHandler
*QXmlSimpleReader::
DTDHandler
() const
重实现: QXmlReader::DTDHandler () const.
另请参阅 setDTDHandler ().
[override virtual]
QXmlContentHandler
*QXmlSimpleReader::
contentHandler
() const
重实现: QXmlReader::contentHandler () const.
另请参阅 setContentHandler ().
[override virtual]
QXmlDeclHandler
*QXmlSimpleReader::
declHandler
() const
重实现: QXmlReader::declHandler () const.
另请参阅 setDeclHandler ().
[override virtual]
QXmlEntityResolver
*QXmlSimpleReader::
entityResolver
() const
重实现: QXmlReader::entityResolver () const.
另请参阅 setEntityResolver ().
[override virtual]
QXmlErrorHandler
*QXmlSimpleReader::
errorHandler
() const
重实现: QXmlReader::errorHandler () const.
另请参阅 setErrorHandler ().
[override virtual]
bool
QXmlSimpleReader::
feature
(const
QString
&
name
,
bool
*
ok
= nullptr) const
重实现: QXmlReader::feature (const QString &name, bool *ok) const.
另请参阅 setFeature ().
[override virtual]
bool
QXmlSimpleReader::
hasFeature
(const
QString
&
name
) const
重实现: QXmlReader::hasFeature (const QString &name) const.
[override virtual]
bool
QXmlSimpleReader::
hasProperty
(const
QString
&
name
) const
重实现: QXmlReader::hasProperty (const QString &name) const.
[override virtual]
QXmlLexicalHandler
*QXmlSimpleReader::
lexicalHandler
() const
重实现: QXmlReader::lexicalHandler () const.
另请参阅 setLexicalHandler ().
[override virtual]
bool
QXmlSimpleReader::
parse
(const
QXmlInputSource
&
input
)
重实现: QXmlReader::parse (const QXmlInputSource &input).
[override virtual]
bool
QXmlSimpleReader::
parse
(const
QXmlInputSource
*
input
)
重实现: QXmlReader::parse (const QXmlInputSource *input).
读取 XML 文档从
input
and parses it in one pass (non-incrementally). Returns
true
若剖析成功;否则返回
false
.
[虚拟]
bool
QXmlSimpleReader::
parse
(const
QXmlInputSource
*
input
,
bool
incremental
)
读取 XML 文档从
input
并剖析它。返回
true
if the parsing is completed successfully; otherwise returns
false
, indicating that an error occurred.
若 incremental is false, this function will return false if the XML file is not read completely. The parsing cannot be continued in this case.
若 incremental is true, the parser does not return false if it reaches the end of the input before 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 input 和 parseContinue () function tries to read from that input source. Therefore, you should not delete the input source input until you no longer need to call parseContinue ().
If this function is called with incremental set to true while an incremental parse is in progress, a new parsing session will be started, and the previous session will be lost.
另请参阅 parseContinue () 和 QTcpSocket .
[虚拟]
bool
QXmlSimpleReader::
parseContinue
()
Continues incremental parsing, taking input from the QXmlInputSource that 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.
返回
false
if 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.
另请参阅 parse (), QXmlInputSource::data (),和 QXmlInputSource::next ().
[override virtual]
void
*QXmlSimpleReader::
property
(const
QString
&
name
,
bool
*
ok
= nullptr) const
重实现: QXmlReader::property (const QString &name, bool *ok) const.
另请参阅 setProperty ().
[override virtual]
void
QXmlSimpleReader::
setContentHandler
(
QXmlContentHandler
*
handler
)
重实现: QXmlReader::setContentHandler (QXmlContentHandler *handler).
另请参阅 contentHandler ().
[override virtual]
void
QXmlSimpleReader::
setDTDHandler
(
QXmlDTDHandler
*
handler
)
重实现: QXmlReader::setDTDHandler (QXmlDTDHandler *handler).
[override virtual]
void
QXmlSimpleReader::
setDeclHandler
(
QXmlDeclHandler
*
handler
)
重实现: QXmlReader::setDeclHandler (QXmlDeclHandler *handler).
另请参阅 declHandler ().
[override virtual]
void
QXmlSimpleReader::
setEntityResolver
(
QXmlEntityResolver
*
handler
)
重实现: QXmlReader::setEntityResolver (QXmlEntityResolver *handler).
另请参阅 entityResolver ().
[override virtual]
void
QXmlSimpleReader::
setErrorHandler
(
QXmlErrorHandler
*
handler
)
重实现: QXmlReader::setErrorHandler (QXmlErrorHandler *handler).
另请参阅 errorHandler ().
[override virtual]
void
QXmlSimpleReader::
setFeature
(const
QString
&
name
,
bool
enable
)
重实现: QXmlReader::setFeature (const QString &name, bool value).
Turns on the feature name if enable is true; otherwise turns it off.
name parameter must be one of the following strings:
特征 | 默认 | 注意事项 |
---|---|---|
http://xml.org/sax/features/namespaces | true | If enabled, namespaces are reported to the content handler. |
http://xml.org/sax/features/namespace-prefixes | false | If enabled, the original prefixed names and attributes used for namespace declarations are reported. |
http://qt-project.org/xml/features/report-whitespace-only-CharData | true | If enabled, CharData that consist of only whitespace characters are reported using QXmlContentHandler::characters (). If disabled, whitespace is silently discarded. |
http://qt-project.org/xml/features/report-start-end-entity | false | If enabled, the parser reports QXmlContentHandler::startEntity() and QXmlContentHandler::endEntity() events, so character data might be reported in chunks. If disabled, the parser does not report these events, but silently substitutes the entities, and reports the character data in one chunk. |
另请参阅 feature () 和 hasFeature ().
[override virtual]
void
QXmlSimpleReader::
setLexicalHandler
(
QXmlLexicalHandler
*
handler
)
重实现: QXmlReader::setLexicalHandler (QXmlLexicalHandler *handler).
另请参阅 lexicalHandler ().
[override virtual]
void
QXmlSimpleReader::
setProperty
(const
QString
&
name
,
void
*
value
)
重实现: QXmlReader::setProperty (const QString &name, void *value).
另请参阅 property ().