• PySide 模块
  • PySide.QtCore
  • 内容表

    上一话题

    QRunnable

    下一话题

    QXmlStreamReader

    QXmlStreamWriter

    概要

    函数

    详细描述

    PySide.QtCore.QXmlStreamWriter class provides an XML writer with a simple streaming API.

    PySide.QtCore.QXmlStreamWriter is the counterpart to PySide.QtCore.QXmlStreamReader 为写入 XML。像其相关类,它操作 PySide.QtCore.QIODevice 指定采用 PySide.QtCore.QXmlStreamWriter.setDevice() . The API is simple and straightforward: for every XML token or event you want to write, the writer provides a specialized function.

    开始文档采用 PySide.QtCore.QXmlStreamWriter.writeStartDocument() and end it with PySide.QtCore.QXmlStreamWriter.writeEndDocument() . This will implicitly close all remaining open tags.

    Element tags are opened with PySide.QtCore.QXmlStreamWriter.writeStartElement() followed by PySide.QtCore.QXmlStreamWriter.writeAttribute() or PySide.QtCore.QXmlStreamWriter.writeAttributes() , element content, and then PySide.QtCore.QXmlStreamWriter.writeEndElement() . A shorter form PySide.QtCore.QXmlStreamWriter.writeEmptyElement() can be used to write empty elements, followed by PySide.QtCore.QXmlStreamWriter.writeAttributes() .

    Element content consists of either characters, entity references or nested elements. It is written with PySide.QtCore.QXmlStreamWriter.writeCharacters() , which also takes care of escaping all forbidden characters and character sequences, PySide.QtCore.QXmlStreamWriter.writeEntityReference() , or subsequent calls to PySide.QtCore.QXmlStreamWriter.writeStartElement() . A convenience method PySide.QtCore.QXmlStreamWriter.writeTextElement() can be used for writing terminal elements that contain nothing but text.

    The following abridged code snippet shows the basic use of the class to write formatted XML with indentation:

    QXmlStreamWriter stream(&output);
    stream.setAutoFormatting(true);
    stream.writeStartDocument();
    ...
    stream.writeStartElement("bookmark");
    stream.writeAttribute("href", "http://www.qtsoftware.com/");
    stream.writeTextElement("title", "Qt Home");
    stream.writeEndElement(); // bookmark
    ...
    stream.writeEndDocument();
    									

    PySide.QtCore.QXmlStreamWriter takes care of prefixing namespaces, all you have to do is specify the namespaceUri when writing elements or attributes. If you must conform to certain prefixes, you can force the writer to use them by declaring the namespaces manually with either PySide.QtCore.QXmlStreamWriter.writeNamespace() or PySide.QtCore.QXmlStreamWriter.writeDefaultNamespace() . Alternatively, you can bypass the stream writer's namespace support and use overloaded methods that take a qualified name instead. The namespace http://www.w3.org/XML/1998/namespace is implicit and mapped to the prefix xml .

    The stream writer can automatically format the generated XML data by adding line-breaks and indentation to empty sections between elements, making the XML data more readable for humans and easier to work with for most source code management systems. The feature can be turned on with the PySide.QtCore.QXmlStreamWriter.autoFormatting() property, and customized with the PySide.QtCore.QXmlStreamWriter.autoFormattingIndent() 特性。

    Other functions are PySide.QtCore.QXmlStreamWriter.writeCDATA() , PySide.QtCore.QXmlStreamWriter.writeComment() , PySide.QtCore.QXmlStreamWriter.writeProcessingInstruction() ,和 PySide.QtCore.QXmlStreamWriter.writeDTD() . Chaining of XML streams is supported with PySide.QtCore.QXmlStreamWriter.writeCurrentToken() .

    默认情况下, PySide.QtCore.QXmlStreamWriter encodes XML in UTF-8. Different encodings can be enforced using PySide.QtCore.QXmlStreamWriter.setCodec() .

    If an error occurs while writing to the underlying device, PySide.QtCore.QXmlStreamWriter.hasError() starts returning true and subsequent writes are ignored.

    QXmlStream 书签范例 illustrates how to use a stream writer to write an XML bookmark file (XBEL) that was previously read in by a PySide.QtCore.QXmlStreamReader .

    class PySide.QtCore. QXmlStreamWriter
    class PySide.QtCore. QXmlStreamWriter ( array )
    class PySide.QtCore. QXmlStreamWriter ( device )
    参数:

    构造流写入器。

    Constructs a stream writer that writes into array . This is the same as creating an xml writer that operates on a PySide.QtCore.QBuffer device which in turn operates on array .

    Constructs a stream writer that writes into device ;

    PySide.QtCore.QXmlStreamWriter. autoFormatting ( )
    返回类型: PySide.QtCore.bool

    返回 true 若自动格式化被启用,否则 false .

    PySide.QtCore.QXmlStreamWriter. autoFormattingIndent ( )
    返回类型: PySide.QtCore.int
    PySide.QtCore.QXmlStreamWriter. codec ( )
    返回类型: PySide.QtCore.QTextCodec

    Returns the codec that is currently assigned to the stream.

    PySide.QtCore.QXmlStreamWriter. device ( )
    返回类型: PySide.QtCore.QIODevice

    返回被当前设备关联的 PySide.QtCore.QXmlStreamWriter , or 0 if no device has been assigned.

    PySide.QtCore.QXmlStreamWriter. hasError ( )
    返回类型: PySide.QtCore.bool

    Returns true if the stream failed to write to the underlying device; otherwise returns false.

    The error status is never reset. Writes happening after the error occurred are ignored, even if the error condition is cleared.

    PySide.QtCore.QXmlStreamWriter. setAutoFormatting ( arg__1 )
    参数: arg__1 PySide.QtCore.bool

    启用自动格式化若 enable is true ,否则禁用它。

    默认值为 false .

    PySide.QtCore.QXmlStreamWriter. setAutoFormattingIndent ( spacesOrTabs )
    参数: spacesOrTabs PySide.QtCore.int
    PySide.QtCore.QXmlStreamWriter. setCodec ( codec )
    参数: codec PySide.QtCore.QTextCodec

    Sets the codec for this stream to codec . The codec is used for encoding any data that is written. By default, PySide.QtCore.QXmlStreamWriter uses UTF-8.

    The encoding information is stored in the initial xml tag which gets written when you call PySide.QtCore.QXmlStreamWriter.writeStartDocument() . Call this function before calling PySide.QtCore.QXmlStreamWriter.writeStartDocument() .

    PySide.QtCore.QXmlStreamWriter. setCodec ( codecName )
    参数: codecName – str

    Sets the codec for this stream to the PySide.QtCore.QTextCodec 为指定编码通过 codecName 。常见值对于 codecName include “ISO 8859-1”, “UTF-8”, and “UTF-16”. If the encoding isn't recognized, nothing happens.

    PySide.QtCore.QXmlStreamWriter. setDevice ( device )
    参数: device PySide.QtCore.QIODevice

    把当前设备设为 device . If you want the stream to write into a PySide.QtCore.QByteArray ,可以创建 PySide.QtCore.QBuffer 设备。

    PySide.QtCore.QXmlStreamWriter. writeAttribute ( qualifiedName , value )
    参数:
    • qualifiedName – unicode
    • value – unicode

    这是重载函数。

    Writes an attribute with qualifiedName and value .

    This function can only be called after PySide.QtCore.QXmlStreamWriter.writeStartElement() before any content is written, or after PySide.QtCore.QXmlStreamWriter.writeEmptyElement() .

    PySide.QtCore.QXmlStreamWriter. writeAttribute ( attribute )
    参数: attribute PySide.QtCore.QXmlStreamAttribute

    这是重载函数。

    写入 attribute .

    This function can only be called after PySide.QtCore.QXmlStreamWriter.writeStartElement() before any content is written, or after PySide.QtCore.QXmlStreamWriter.writeEmptyElement() .

    PySide.QtCore.QXmlStreamWriter. writeAttribute ( namespaceUri , name , value )
    参数:
    • namespaceUri – unicode
    • name – unicode
    • value – unicode

    Writes an attribute with name and value , prefixed for the specified namespaceUri . If the namespace has not been declared yet, PySide.QtCore.QXmlStreamWriter will generate a namespace declaration for it.

    This function can only be called after PySide.QtCore.QXmlStreamWriter.writeStartElement() before any content is written, or after PySide.QtCore.QXmlStreamWriter.writeEmptyElement() .

    PySide.QtCore.QXmlStreamWriter. writeAttributes ( attributes )
    参数: attributes PySide.QtCore.QXmlStreamAttributes

    Writes the attribute vector attributes . If a namespace referenced in an attribute not been declared yet, PySide.QtCore.QXmlStreamWriter will generate a namespace declaration for it.

    This function can only be called after PySide.QtCore.QXmlStreamWriter.writeStartElement() before any content is written, or after PySide.QtCore.QXmlStreamWriter.writeEmptyElement() .

    PySide.QtCore.QXmlStreamWriter. writeCDATA ( text )
    参数: text – unicode

    写入 text as CDATA section. If text contains the forbidden character sequence “]]>”, it is split into different CDATA sections.

    This function mainly exists for completeness. Normally you should not need use it, because PySide.QtCore.QXmlStreamWriter.writeCharacters() automatically escapes all non-content characters.

    PySide.QtCore.QXmlStreamWriter. writeCharacters ( text )
    参数: text – unicode

    写入 text . The characters “<”, “&”, and “”” are escaped as entity references “<”, “&, and “"”. To avoid the forbidden sequence “]]>”, “>” is also escaped as “>”.

    PySide.QtCore.QXmlStreamWriter. writeComment ( text )
    参数: text – unicode

    写入 text as XML comment, where text must not contain the forbidden sequence “–” or end with “-”. Note that XML does not provide any way to escape “-” in a comment.

    PySide.QtCore.QXmlStreamWriter. writeCurrentToken ( reader )
    参数: reader PySide.QtCore.QXmlStreamReader

    Writes the current state of the reader . All possible valid states are supported.

    The purpose of this function is to support chained processing of XML data.

    PySide.QtCore.QXmlStreamWriter. writeDTD ( dtd )
    参数: dtd – unicode

    Writes a DTD section. The dtd represents the entire doctypedecl production from the XML 1.0 specification.

    PySide.QtCore.QXmlStreamWriter. writeDefaultNamespace ( namespaceUri )
    参数: namespaceUri – unicode

    Writes a default namespace declaration for namespaceUri .

    PySide.QtCore.QXmlStreamWriter.writeStartElement() or PySide.QtCore.QXmlStreamWriter.writeEmptyElement() was called, the declaration applies to the current element; otherwise it applies to the next child element.

    Note that the namespaces http://www.w3.org/XML/1998/namespace (bound to xmlns ) and http://www.w3.org/2000/xmlns/ (bound to xml ) by definition cannot be declared as default.

    PySide.QtCore.QXmlStreamWriter. writeEmptyElement ( namespaceUri , name )
    参数:
    • namespaceUri – unicode
    • name – unicode

    Writes an empty element with name , prefixed for the specified namespaceUri . If the namespace has not been declared, PySide.QtCore.QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls to PySide.QtCore.QXmlStreamWriter.writeAttribute() will add attributes to this element.

    PySide.QtCore.QXmlStreamWriter. writeEmptyElement ( qualifiedName )
    参数: qualifiedName – unicode

    这是重载函数。

    Writes an empty element with qualified name qualifiedName . Subsequent calls to PySide.QtCore.QXmlStreamWriter.writeAttribute() will add attributes to this element.

    PySide.QtCore.QXmlStreamWriter. writeEndDocument ( )

    Closes all remaining open start elements and writes a newline.

    PySide.QtCore.QXmlStreamWriter. writeEndElement ( )

    Closes the previous start element.

    PySide.QtCore.QXmlStreamWriter. writeEntityReference ( name )
    参数: name – unicode

    Writes the entity reference name to the stream, as “&``name`` ;”.

    PySide.QtCore.QXmlStreamWriter. writeNamespace ( namespaceUri [ , prefix="" ] )
    参数:
    • namespaceUri – unicode
    • prefix – unicode

    Writes a namespace declaration for namespaceUri with prefix 。若 prefix is empty, PySide.QtCore.QXmlStreamWriter assigns a unique prefix consisting of the letter ‘n' followed by a number.

    PySide.QtCore.QXmlStreamWriter.writeStartElement() or PySide.QtCore.QXmlStreamWriter.writeEmptyElement() was called, the declaration applies to the current element; otherwise it applies to the next child element.

    Note that the prefix xml is both predefined and reserved for http://www.w3.org/XML/1998/namespace , which in turn cannot be bound to any other prefix. The prefix xmlns and its URI http://www.w3.org/2000/xmlns/ are used for the namespace mechanism itself and thus completely forbidden in declarations.

    PySide.QtCore.QXmlStreamWriter. writeProcessingInstruction ( target [ , data="" ] )
    参数:
    • target – unicode
    • data – unicode

    Writes an XML processing instruction with target and data ,其中 data must not contain the sequence ”?>”.

    PySide.QtCore.QXmlStreamWriter. writeStartDocument ( version )
    参数: version – unicode

    Writes a document start with the XML version number version .

    PySide.QtCore.QXmlStreamWriter. writeStartDocument ( version , standalone )
    参数:
    • version – unicode
    • standalone PySide.QtCore.bool

    Writes a document start with the XML version number version and a standalone attribute standalone .

    PySide.QtCore.QXmlStreamWriter. writeStartDocument ( )

    这是重载函数。

    Writes a document start with XML version number “1.0”. This also writes the encoding information.

    PySide.QtCore.QXmlStreamWriter. writeStartElement ( qualifiedName )
    参数: qualifiedName – unicode

    这是重载函数。

    Writes a start element with qualifiedName . Subsequent calls to PySide.QtCore.QXmlStreamWriter.writeAttribute() will add attributes to this element.

    PySide.QtCore.QXmlStreamWriter. writeStartElement ( namespaceUri , name )
    参数:
    • namespaceUri – unicode
    • name – unicode

    Writes a start element with name , prefixed for the specified namespaceUri . If the namespace has not been declared yet, PySide.QtCore.QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls to PySide.QtCore.QXmlStreamWriter.writeAttribute() will add attributes to this element.

    PySide.QtCore.QXmlStreamWriter. writeTextElement ( namespaceUri , name , text )
    参数:
    • namespaceUri – unicode
    • name – unicode
    • text – unicode

    Writes a text element with name , prefixed for the specified namespaceUri ,和 text . If the namespace has not been declared, PySide.QtCore.QXmlStreamWriter will generate a namespace declaration for it.

    This is a convenience function equivalent to:

    writeStartElement(namespaceUri, name)
    writeCharacters(text)
    writeEndElement()
    											
    PySide.QtCore.QXmlStreamWriter. writeTextElement ( qualifiedName , text )
    参数:
    • qualifiedName – unicode
    • text – unicode

    这是重载函数。

    Writes a text element with qualifiedName and text .

    This is a convenience function equivalent to:

    writeStartElement(qualifiedName)
    writeCharacters(text)
    writeEndElement()