QXmlStreamWriter 類

The QXmlStreamWriter class provides an XML writer with a simple streaming API. 更多...

頭: #include <QXmlStreamWriter>
qmake: QT += core
Since: Qt 4.3

注意: 此類的所有函數 可重入 .

特性

公共函數

QXmlStreamWriter ()
QXmlStreamWriter (QIODevice * device )
QXmlStreamWriter (QByteArray * array )
QXmlStreamWriter (QString * string )
~QXmlStreamWriter ()
bool autoFormatting () const
int autoFormattingIndent () const
QTextCodec * codec () const
QIODevice * device () const
bool hasError () const
void setAutoFormatting (bool enable )
void setAutoFormattingIndent (int spacesOrTabs )
void setCodec (QTextCodec * codec )
void setCodec (const char * codecName )
void setDevice (QIODevice * device )
void writeAttribute (const QString & namespaceUri , const QString & name , const QString & value )
void writeAttribute (const QString & qualifiedName , const QString & value )
void writeAttribute (const QXmlStreamAttribute & attribute )
void writeAttributes (const QXmlStreamAttributes & 屬性 )
void writeCDATA (const QString & text )
void writeCharacters (const QString & text )
void writeComment (const QString & text )
void writeCurrentToken (const QXmlStreamReader & reader )
void writeDTD (const QString & dtd )
void writeDefaultNamespace (const QString & namespaceUri )
void writeEmptyElement (const QString & namespaceUri , const QString & name )
void writeEmptyElement (const QString & qualifiedName )
void writeEndDocument ()
void writeEndElement ()
void writeEntityReference (const QString & name )
void writeNamespace (const QString & namespaceUri , const QString & prefix = QString())
void writeProcessingInstruction (const QString & target , const QString & data = QString())
void writeStartDocument (const QString & version )
void writeStartDocument ()
void writeStartDocument (const QString & version , bool standalone )
void writeStartElement (const QString & namespaceUri , const QString & name )
void writeStartElement (const QString & qualifiedName )
void writeTextElement (const QString & namespaceUri , const QString & name , const QString & text )
void writeTextElement (const QString & qualifiedName , const QString & text )

詳細描述

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

QXmlStreamWriter is the counterpart to QXmlStreamReader 為寫入 XML。像其相關類,它操作 QIODevice 指定采用 setDevice ()。API 簡單明瞭:為想要編寫每個 XML 令牌或事件,寫入器提供專用功能。

開始文檔采用 writeStartDocument () 和結束它采用 writeEndDocument (). This will implicitly close all remaining open tags.

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

Element content consists of either characters, entity references or nested elements. It is written with writeCharacters (), which also takes care of escaping all forbidden characters and character sequences, writeEntityReference (), or subsequent calls to writeStartElement (). A convenience method 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://qt-project.org/");
    stream.writeTextElement("title", "Qt Project");
    stream.writeEndElement(); // bookmark
    ...
    stream.writeEndDocument();
					

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 writeNamespace () 或 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 autoFormatting property, and customized with the autoFormattingIndent 特性。

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

默認情況下, QXmlStreamWriter encodes XML in UTF-8. Different encodings can be enforced using setCodec ().

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

The QXmlStream 書簽範例 illustrates how to use a stream writer to write an XML bookmark file (XBEL) that was previously read in by a QXmlStreamReader .

特性文檔編製

autoFormatting : bool

The auto-formatting flag of the stream writer

This property controls whether or not the stream writer automatically formats the generated XML data. If enabled, the writer automatically adds line-breaks and indentation to empty sections between elements (ignorable whitespace). The main purpose of auto-formatting is to split the data into several lines, and to increase readability for a human reader. The indentation depth can be controlled through the autoFormattingIndent 特性。

默認情況下,自動格式化被禁用。

該特性在 Qt 4.4 引入。

訪問函數:

bool autoFormatting () const
void setAutoFormatting (bool enable )

autoFormattingIndent : int

This property holds the number of spaces or tabs used for indentation when auto-formatting is enabled. Positive numbers indicate spaces, negative numbers tabs.

默認縮進為 4。

該特性在 Qt 4.4 引入。

訪問函數:

int autoFormattingIndent () const
void setAutoFormattingIndent (int spacesOrTabs )

另請參閱 autoFormatting .

成員函數文檔編製

QXmlStreamWriter:: QXmlStreamWriter ()

構造流寫入器。

另請參閱 setDevice ().

QXmlStreamWriter:: QXmlStreamWriter ( QIODevice * device )

Constructs a stream writer that writes into device ;

QXmlStreamWriter:: QXmlStreamWriter ( QByteArray * array )

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

QXmlStreamWriter:: QXmlStreamWriter ( QString * string )

Constructs a stream writer that writes into string .

Note that when writing to QString , QXmlStreamWriter ignores the codec set with setCodec (). See that function for more information.

QXmlStreamWriter:: ~QXmlStreamWriter ()

析構函數。

bool QXmlStreamWriter:: autoFormatting () const

返迴 true 若自動格式化被啓用,否則 false .

該函數在 Qt 4.4 引入。

注意: getter 函數對於特性 autoFormatting .

另請參閱 setAutoFormatting ().

QTextCodec *QXmlStreamWriter:: codec () const

Returns the codec that is currently assigned to the stream.

另請參閱 setCodec ().

QIODevice *QXmlStreamWriter:: device () const

返迴被當前設備關聯的 QXmlStreamWriter , or 0 if no device has been assigned.

另請參閱 setDevice ().

bool QXmlStreamWriter:: hasError () const

返迴 true 若寫入失敗。

This can happen if the stream failed to write to the underlying device or if the data to be written contained invalid characters.

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

void QXmlStreamWriter:: setAutoFormatting ( bool enable )

啓用自動格式化若 enable is true ,否則禁用它。

默認值為 false .

該函數在 Qt 4.4 引入。

注意: setter 函數對於特性 autoFormatting .

另請參閱 autoFormatting ().

void QXmlStreamWriter:: setCodec ( QTextCodec * codec )

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

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

注意: When writing the XML to a QString , the codec information is ignored and the XML header will not include any encoding information, since all QStrings are UTF-16. If you later convert the QString to an 8-bit format, you must arrange for the encoding information to be transmitted out-of-band.

另請參閱 codec ().

void QXmlStreamWriter:: setCodec (const char * codecName )

Sets the codec for this stream to the QTextCodec 為指定編碼通過 codecName 。常見值對於 codecName 包括 ISO 8859-1、UTF-8 及 UTF-16。若編碼無法識彆,則什麼都不發生。

注意: When writing the XML to a QString , the codec information is ignored and the XML header will not include any encoding information, since all QStrings are UTF-16. If you later convert the QString to an 8-bit format, you must arrange for the encoding information to be transmitted out-of-band.

另請參閱 QTextCodec::codecForName ().

void QXmlStreamWriter:: setDevice ( QIODevice * device )

把當前設備設為 device . If you want the stream to write into a QByteArray ,可以創建 QBuffer 設備。

另請參閱 device ().

void QXmlStreamWriter:: writeAttribute (const QString & namespaceUri , const QString & name , const QString & value )

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

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

void QXmlStreamWriter:: writeAttribute (const QString & qualifiedName , const QString & value )

這是重載函數。

Writes an attribute with qualifiedName and value .

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

void QXmlStreamWriter:: writeAttribute (const QXmlStreamAttribute & attribute )

這是重載函數。

寫入 attribute .

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

void QXmlStreamWriter:: writeAttributes (const QXmlStreamAttributes & 屬性 )

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

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

另請參閱 writeAttribute () 和 writeNamespace ().

void QXmlStreamWriter:: writeCDATA (const QString & text )

寫入 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 writeCharacters () automatically escapes all non-content characters.

void QXmlStreamWriter:: writeCharacters (const QString & text )

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

另請參閱 writeEntityReference ().

void QXmlStreamWriter:: writeComment (const QString & text )

寫入 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.

void QXmlStreamWriter:: writeCurrentToken (const QXmlStreamReader & reader )

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.

另請參閱 QXmlStreamReader::tokenType ().

void QXmlStreamWriter:: writeDTD (const QString & dtd )

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

void QXmlStreamWriter:: writeDefaultNamespace (const QString & namespaceUri )

Writes a default namespace declaration for namespaceUri .

writeStartElement () 或 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 ) 和 http://www.w3.org/2000/xmlns/ (bound to xml ) by definition cannot be declared as default.

void QXmlStreamWriter:: writeEmptyElement (const QString & namespaceUri , const QString & name )

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

另請參閱 writeNamespace ().

void QXmlStreamWriter:: writeEmptyElement (const QString & qualifiedName )

這是重載函數。

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

void QXmlStreamWriter:: writeEndDocument ()

Closes all remaining open start elements and writes a newline.

另請參閱 writeStartDocument ().

void QXmlStreamWriter:: writeEndElement ()

Closes the previous start element.

另請參閱 writeStartElement ().

void QXmlStreamWriter:: writeEntityReference (const QString & name )

Writes the entity reference name to the stream, as "& name ;".

void QXmlStreamWriter:: writeNamespace (const QString & namespaceUri , const QString & prefix = QString())

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

writeStartElement () 或 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.

void QXmlStreamWriter:: writeProcessingInstruction (const QString & target , const QString & data = QString())

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

void QXmlStreamWriter:: writeStartDocument (const QString & version )

Writes a document start with the XML version number version .

另請參閱 writeEndDocument ().

void QXmlStreamWriter:: writeStartDocument ()

這是重載函數。

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

該函數在 Qt 4.5 引入。

另請參閱 writeEndDocument () 和 setCodec ().

void QXmlStreamWriter:: writeStartDocument (const QString & version , bool standalone )

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

該函數在 Qt 4.5 引入。

另請參閱 writeEndDocument ().

void QXmlStreamWriter:: writeStartElement (const QString & namespaceUri , const QString & name )

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

另請參閱 writeNamespace (), writeEndElement (),和 writeEmptyElement ().

void QXmlStreamWriter:: writeStartElement (const QString & qualifiedName )

這是重載函數。

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

另請參閱 writeEndElement () 和 writeEmptyElement ().

void QXmlStreamWriter:: writeTextElement (const QString & namespaceUri , const QString & name , const QString & text )

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

This is a convenience function equivalent to:

        writeStartElement(namespaceUri, name);
        writeCharacters(text);
        writeEndElement();
					

void QXmlStreamWriter:: writeTextElement (const QString & qualifiedName , const QString & text )

這是重載函數。

Writes a text element with qualifiedName and text .

This is a convenience function equivalent to:

        writeStartElement(qualifiedName);
        writeCharacters(text);
        writeEndElement();