QXmlStreamReaderclass provides a fast parser for reading well-formed XML via a simple streaming API. 更多 …
def
addData
(data)
def
addData
(data)
def
addData
(data)
def
addExtraNamespaceDeclaration
(extraNamespaceDeclaraction)
def
addExtraNamespaceDeclarations
(extraNamespaceDeclaractions)
def
atEnd
()
def
attributes
()
def
characterOffset
()
def
clear
()
def
columnNumber
()
def
device
()
def
documentEncoding
()
def
documentVersion
()
def
dtdName
()
def
dtdPublicId
()
def
dtdSystemId
()
def
entityDeclarations
()
def
entityExpansionLimit
()
def
entityResolver
()
def
error
()
def
errorString
()
def
hasError
()
def
isCDATA
()
def
isCharacters
()
def
isComment
()
def
isDTD
()
def
isEndDocument
()
def
isEndElement
()
def
isEntityReference
()
def
isProcessingInstruction
()
def
isStandaloneDocument
()
def
isStartDocument
()
def
isStartElement
()
def
isWhitespace
()
def
lineNumber
()
def
name
()
def
namespaceDeclarations
()
def
namespaceProcessing
()
def
namespaceUri
()
def
notationDeclarations
()
def
prefix
()
def
processingInstructionData
()
def
processingInstructionTarget
()
def
qualifiedName
()
def
raiseError
([message=””])
def
readElementText
([behaviour=ErrorOnUnexpectedElement])
def
readNext
()
def
readNextStartElement
()
def
setDevice
(device)
def
setEntityExpansionLimit
(limit)
def
setEntityResolver
(resolver)
def
setNamespaceProcessing
(arg__1)
def
skipCurrentElement
()
def
text
()
def
tokenString
()
def
tokenType
()
QXmlStreamReaderis a faster and more convenient replacement for Qt’s own SAX parser (seeQXmlSimpleReader). In some cases it might also be a faster and more convenient alternative for use in applications that would otherwise use a DOM tree (seeQDomDocument).QXmlStreamReaderreads data either from aQIODevice(见setDevice()), or from a rawQByteArray(见addData()).Qt 提供
QXmlStreamWriter为写入 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
QXmlStreamReaderand SAX is how these XML tokens are reported. With SAX, the application must provide handlers (callback functions) that receive so-called XML events from the parser at the parser’s convenience. WithQXmlStreamReader, the application code itself drives the loop and pulls tokens from the reader, one after another, as it needs them. This is done by callingreadNext(), where the reader reads from the input stream until it completes the next token, at which point it returns thetokenType(). A set of convenient functions includingisStartElement()andtext()can then be used to examine the token to obtain information about what has been read. The big advantage of this pulling approach is the possibility to build recursive descent parsers with it, 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看起来像这样:xml = QXmlStreamReader() ... while not xml.atEnd(): xml.readNext(); ... # do processing if xml.hasError(): ... # do error handling
QXmlStreamReaderis 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.若发生错误当剖析时,
atEnd()andhasError()return true, anderror()returns the error that occurred. The functionserrorString(),lineNumber(),columnNumber(),和characterOffset()are for constructing an appropriate error or warning message. To simplify application code,QXmlStreamReadercontains araiseError()mechanism that lets you raise custom errors that trigger the same error handling described.QXmlStream 书签范例 illustrates how to use the recursive descent technique to read an XML bookmark file (XBEL) with a stream reader.
QXmlStream understands and resolves XML namespaces. E.g. in case of a
StartElement,namespaceUri()returns the namespace the element is in, andname()returns the element’s local name. The combination ofnamespaceUriand name uniquely identifies an element. If a namespace prefix was not declared in the XML entities parsed by the reader, thenamespaceUriis 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’sprefix()followed by colon followed by the element’s localname()- exactly like the element appears in the raw XML data. Since the mappingnamespaceUrito 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特性。
QXmlStreamReaderis an incremental parser. It can handle the case where the document can’t 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 aPrematureEndOfDocumentError. When more data arrives, either because of a call toaddData()or because more data is available through the networkdevice(), the reader recovers from thePrematureEndOfDocumentErrorerror and continues parsing the new data with the next call toreadNext().For example, if your application reads data from the network using a
network access manager, you would issue anetwork requestto the manager and receive anetwork replyin return. Since aQNetworkReply是QIODevice, you connect itsreadyRead()signal to a custom slot, e.g.slotReadyRead()in the code snippet shown in the discussion forQNetworkAccessManager. In this slot, you read all available data withreadAll()and pass it to the XML stream reader usingaddData(). Then you call your custom parsing function that reads the XML events from the reader.
QXmlStreamReaderis 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,QXmlStreamReaderavoids 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 asQStringRefrather than realQString对象。QStringRefis a thin wrapper aroundQStringsubstrings that provides a subset of theQStringAPI without the memory allocation and reference-counting overhead. CallingtoString()on any of those objects returns an equivalent realQString对象。
QXmlStreamReader
¶
QXmlStreamReader(device)
QXmlStreamReader(data)
QXmlStreamReader(data)
QXmlStreamReader(data)
- param device
- param data
构造流读取器。
另请参阅
创建的新流读取器读取自
device
.
另请参阅
创建的新流读取器读取自
data
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
TokenType
¶
此枚举指定读取器刚刚读取的令牌类型。
|
常量 |
描述 |
|---|---|
|
QXmlStreamReader.NoToken |
读取器尚未读取任何内容。 |
|
QXmlStreamReader.Invalid |
发生错误,报告在
|
|
QXmlStreamReader.StartDocument |
The reader reports the XML version number in
|
|
QXmlStreamReader.EndDocument |
The reader reports the end of the document. |
|
QXmlStreamReader.StartElement |
The reader reports the start of an element with
|
|
QXmlStreamReader.EndElement |
The reader reports the end of an element with
|
|
QXmlStreamReader.Characters |
The reader reports characters in
|
|
QXmlStreamReader.Comment |
读取器报告注释按
|
|
QXmlStreamReader.DTD |
The reader reports a DTD in
|
|
QXmlStreamReader.EntityReference |
The reader reports an entity reference that could not be resolved. The name of the reference is reported in
|
|
QXmlStreamReader.ProcessingInstruction |
The reader reports a processing instruction in
|
PySide2.QtCore.QXmlStreamReader.
ReadElementTextBehaviour
¶
此枚举指定不同行为在
readElementText()
.
|
常量 |
描述 |
|---|---|
|
QXmlStreamReader.ErrorOnUnexpectedElement |
Raise an
|
|
QXmlStreamReader.IncludeChildElements |
Recursively include the text from child elements. |
|
QXmlStreamReader.SkipChildElements |
跳过子级元素。 |
4.6 版新增。
PySide2.QtCore.QXmlStreamReader.
Error
¶
此枚举指定不同错误情况
|
常量 |
描述 |
|---|---|
|
QXmlStreamReader.NoError |
没有发生错误。 |
|
QXmlStreamReader.CustomError |
引发自定义错误采有
|
|
QXmlStreamReader.NotWellFormedError |
The parser internally raised an error due to the read XML not being well-formed. |
|
QXmlStreamReader.PrematureEndOfDocumentError |
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
|
|
QXmlStreamReader.UnexpectedElementError |
The parser encountered an element that was different to those it expected. |
PySide2.QtCore.QXmlStreamReader.
addData
(
data
)
¶
data
–
QByteArray
PySide2.QtCore.QXmlStreamReader.
addData
(
data
)
¶
data – unicode
PySide2.QtCore.QXmlStreamReader.
addData
(
data
)
¶
data – str
添加更多
data
for the reader to read. This function does nothing if the reader has a
device()
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
addExtraNamespaceDeclaration
(
extraNamespaceDeclaraction
)
¶
extraNamespaceDeclaraction
–
QXmlStreamNamespaceDeclaration
添加
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.
PySide2.QtCore.QXmlStreamReader.
addExtraNamespaceDeclarations
(
extraNamespaceDeclaractions
)
¶
extraNamespaceDeclaractions –
Adds a vector of declarations specified by
extraNamespaceDeclarations
.
PySide2.QtCore.QXmlStreamReader.
atEnd
(
)
¶
bool
返回
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 and
hasError()
return true and
error()
返回
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, will return false once more data is available.
另请参阅
PySide2.QtCore.QXmlStreamReader.
attributes
(
)
¶
返回属性为
StartElement
.
PySide2.QtCore.QXmlStreamReader.
characterOffset
(
)
¶
qint64
返回当前字符偏移,从 0 开始。
PySide2.QtCore.QXmlStreamReader.
clear
(
)
¶
移除任何
device()
or data from the reader and resets its internal state to the initial state.
另请参阅
PySide2.QtCore.QXmlStreamReader.
columnNumber
(
)
¶
qint64
返回当前列号,从 0 开始。
PySide2.QtCore.QXmlStreamReader.
device
(
)
¶
返回被当前设备关联的
QXmlStreamReader
,或
None
若没有设备被赋值。
另请参阅
PySide2.QtCore.QXmlStreamReader.
documentEncoding
(
)
¶
QStringRef
若
tokenType()
is
StartDocument
, this function returns the encoding string as specified in the XML declaration. Otherwise an empty string is returned.
PySide2.QtCore.QXmlStreamReader.
documentVersion
(
)
¶
QStringRef
若
tokenType()
is
StartDocument
, this function returns the version string as specified in the XML declaration. Otherwise an empty string is returned.
PySide2.QtCore.QXmlStreamReader.
dtdName
(
)
¶
QStringRef
若
tokenType()
is
DTD
, this function returns the DTD’s name. Otherwise an empty string is returned.
PySide2.QtCore.QXmlStreamReader.
dtdPublicId
(
)
¶
QStringRef
若
tokenType()
is
DTD
, this function returns the DTD’s public identifier. Otherwise an empty string is returned.
PySide2.QtCore.QXmlStreamReader.
dtdSystemId
(
)
¶
QStringRef
若
tokenType()
is
DTD
, this function returns the DTD’s system identifier. Otherwise an empty string is returned.
PySide2.QtCore.QXmlStreamReader.
entityDeclarations
(
)
¶
若
tokenType()
is
DTD
, this function returns the DTD’s unparsed (external) entity declarations. Otherwise an empty vector is returned.
QXmlStreamEntityDeclarations
class is defined to be a
QVector
of
QXmlStreamEntityDeclaration
.
PySide2.QtCore.QXmlStreamReader.
entityExpansionLimit
(
)
¶
int
Returns the maximum amount of characters a single entity is allowed to expand into. If a single entity expands past the given limit, the document is not considered well formed.
PySide2.QtCore.QXmlStreamReader.
entityResolver
(
)
¶
返回实体解析器,或
None
若没有实体解析器。
另请参阅
PySide2.QtCore.QXmlStreamReader.
errorString
(
)
¶
unicode
返回错误消息,设置采用
raiseError()
.
PySide2.QtCore.QXmlStreamReader.
hasError
(
)
¶
bool
返回
true
若有发生错误,否则
false
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
isCDATA
(
)
¶
bool
返回
true
若读取器报告源自 CDATA 区间的字符;否则返回
false
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
isCharacters
(
)
¶
bool
返回
true
if
tokenType()
等于
Characters
;否则返回
false
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
isComment
(
)
¶
bool
返回
true
if
tokenType()
等于
Comment
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isDTD
(
)
¶
bool
返回
true
if
tokenType()
等于
DTD
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isEndDocument
(
)
¶
bool
返回
true
if
tokenType()
等于
EndDocument
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isEndElement
(
)
¶
bool
返回
true
if
tokenType()
等于
EndElement
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isEntityReference
(
)
¶
bool
返回
true
if
tokenType()
等于
EntityReference
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isProcessingInstruction
(
)
¶
bool
返回
true
if
tokenType()
等于
ProcessingInstruction
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isStandaloneDocument
(
)
¶
bool
返回
true
if this document has been declared standalone in the XML declaration; otherwise returns
false
.
若未剖析 XML 声明,此函数返回
false
.
PySide2.QtCore.QXmlStreamReader.
isStartDocument
(
)
¶
bool
返回
true
if
tokenType()
等于
StartDocument
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isStartElement
(
)
¶
bool
返回
true
if
tokenType()
等于
StartElement
;否则返回
false
.
PySide2.QtCore.QXmlStreamReader.
isWhitespace
(
)
¶
bool
返回
true
if the reader reports characters that only consist of white-space; otherwise returns
false
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
lineNumber
(
)
¶
qint64
返回当前行号,从 1 开始。
PySide2.QtCore.QXmlStreamReader.
name
(
)
¶
QStringRef
返回本地名称为
StartElement
,
EndElement
,或
EntityReference
.
PySide2.QtCore.QXmlStreamReader.
namespaceDeclarations
(
)
¶
若
tokenType()
is
StartElement
, this function returns the element’s namespace declarations. Otherwise an empty vector is returned.
QXmlStreamNamespaceDeclarations
class is defined to be a
QVector
of
QXmlStreamNamespaceDeclaration
.
PySide2.QtCore.QXmlStreamReader.
namespaceProcessing
(
)
¶
bool
PySide2.QtCore.QXmlStreamReader.
namespaceUri
(
)
¶
QStringRef
Returns the of a
StartElement
or
EndElement
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
notationDeclarations
(
)
¶
若
tokenType()
is
DTD
, this function returns the DTD’s notation declarations. Otherwise an empty vector is returned.
QXmlStreamNotationDeclarations
class is defined to be a
QVector
of
QXmlStreamNotationDeclaration
.
PySide2.QtCore.QXmlStreamReader.
prefix
(
)
¶
QStringRef
返回前缀为
StartElement
or
EndElement
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
processingInstructionData
(
)
¶
QStringRef
返回数据为
ProcessingInstruction
.
PySide2.QtCore.QXmlStreamReader.
processingInstructionTarget
(
)
¶
QStringRef
Returns the target of a
ProcessingInstruction
.
PySide2.QtCore.QXmlStreamReader.
qualifiedName
(
)
¶
QStringRef
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 , but the resolved
namespaceUri()
and the attribute’s local
name()
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
raiseError
(
[
message=""
]
)
¶
message – unicode
引发自定义错误采用可选错误
message
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
readElementText
(
[
behaviour=ErrorOnUnexpectedElement
]
)
¶
behaviour
–
ReadElementTextBehaviour
unicode
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 current token (see
tokenType()
) after having called this function is
EndElement
.
函数串联
text()
when it reads either
Characters
or
EntityReference
令牌,但跳过
ProcessingInstruction
and
Comment
. If the current token is not
StartElement
, an empty string is returned.
behaviour
defines what happens in case anything else is read before reaching
EndElement
. The function 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 (default).
PySide2.QtCore.QXmlStreamReader.
readNext
(
)
¶
读取下一令牌并返回其类型。
With one exception, once an
error()
is reported by , further reading of the XML stream is not possible. Then
atEnd()
返回
true
,
hasError()
返回
true
, and this function returns
Invalid
.
The exception is when
error()
返回
PrematureEndOfDocumentError
. This error is reported when the end of an otherwise well-formed chunk of XML is reached, but the chunk doesn’t 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()
.
另请参阅
PySide2.QtCore.QXmlStreamReader.
readNextStartElement
(
)
¶
bool
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 QXmlStream 书签范例 makes extensive use of this function.
另请参阅
PySide2.QtCore.QXmlStreamReader.
setDevice
(
device
)
¶
device
–
QIODevice
把当前设备设为
device
。设置设备将流重置到其初始状态。
PySide2.QtCore.QXmlStreamReader.
setEntityExpansionLimit
(
limit
)
¶
limit
–
int
Sets the maximum amount of characters a single entity is allowed to expand into to
limit
. If a single entity expands past the given limit, the document is not considered well formed.
The limit is there to prevent DoS attacks when loading unknown XML documents where recursive entity expansion could otherwise exhaust all available memory.
此特性的默认值为 4096 字符。
另请参阅
PySide2.QtCore.QXmlStreamReader.
setEntityResolver
(
resolver
)
¶
resolver
–
QXmlStreamEntityResolver
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
None
被设置。
另请参阅
PySide2.QtCore.QXmlStreamReader.
setNamespaceProcessing
(
arg__1
)
¶
arg__1
–
bool
PySide2.QtCore.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.
PySide2.QtCore.QXmlStreamReader.
text
(
)
¶
QStringRef
Returns the text of
Characters
,
Comment
,
DTD
,或
EntityReference
.
PySide2.QtCore.QXmlStreamReader.
tokenString
(
)
¶
unicode
Returns the reader’s current token as string.
另请参阅
PySide2.QtCore.QXmlStreamReader.
tokenType
(
)
¶
返回当前令牌类型。
当前令牌也可以查询采用方便函数
isStartDocument()
,
isEndDocument()
,
isStartElement()
,
isEndElement()
,
isCharacters()
,
isComment()
,
isDTD()
,
isEntityReference()
,和
isProcessingInstruction()
.
另请参阅