Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

QXmlStreamWriter Class

TheQXmlStreamWriter class provides an XML writer with a simple streaming API.More...

Header:#include <QXmlStreamWriter>
Since: Qt 4.3

Note: All functions in this class arereentrant.

Properties

Public Functions

QXmlStreamWriter()
QXmlStreamWriter(QIODevice * device)
QXmlStreamWriter(QByteArray * array)
QXmlStreamWriter(QString * string)
~QXmlStreamWriter()
boolautoFormatting() const
intautoFormattingIndent() const
QTextCodec *codec() const
QIODevice *device() const
boolhasError() const
voidsetAutoFormatting(bool enable)
voidsetAutoFormattingIndent(int spacesOrTabs)
voidsetCodec(QTextCodec * codec)
voidsetCodec(const char * codecName)
voidsetDevice(QIODevice * device)
voidwriteAttribute(const QString & namespaceUri, const QString & name, const QString & value)
voidwriteAttribute(const QString & qualifiedName, const QString & value)
voidwriteAttribute(const QXmlStreamAttribute & attribute)
voidwriteAttributes(const QXmlStreamAttributes & attributes)
voidwriteCDATA(const QString & text)
voidwriteCharacters(const QString & text)
voidwriteComment(const QString & text)
voidwriteCurrentToken(const QXmlStreamReader & reader)
voidwriteDTD(const QString & dtd)
voidwriteDefaultNamespace(const QString & namespaceUri)
voidwriteEmptyElement(const QString & namespaceUri, const QString & name)
voidwriteEmptyElement(const QString & qualifiedName)
voidwriteEndDocument()
voidwriteEndElement()
voidwriteEntityReference(const QString & name)
voidwriteNamespace(const QString & namespaceUri, const QString & prefix = QString())
voidwriteProcessingInstruction(const QString & target, const QString & data = QString())
voidwriteStartDocument(const QString & version)
voidwriteStartDocument(const QString & version, bool standalone)
voidwriteStartDocument()
voidwriteStartElement(const QString & namespaceUri, const QString & name)
voidwriteStartElement(const QString & qualifiedName)
voidwriteTextElement(const QString & namespaceUri, const QString & name, const QString & text)
voidwriteTextElement(const QString & qualifiedName, const QString & text)

Detailed Description

TheQXmlStreamWriter class provides an XML writer with a simple streaming API.

QXmlStreamWriter is the counterpart toQXmlStreamReader for writing XML. Like its related class, it operates on aQIODevice specified withsetDevice(). The API is simple and straightforward: for every XML token or event you want to write, the writer provides a specialized function.

You start a document withwriteStartDocument() and end it withwriteEndDocument(). This will implicitly close all remaining open tags.

Element tags are opened withwriteStartElement() followed bywriteAttribute() orwriteAttributes(), element content, and thenwriteEndElement(). A shorter formwriteEmptyElement() can be used to write empty elements, followed bywriteAttributes().

Element content consists of either characters, entity references or nested elements. It is written withwriteCharacters(), which also takes care of escaping all forbidden characters and character sequences,writeEntityReference(), or subsequent calls towriteStartElement(). A convenience methodwriteTextElement() 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.nokia.com/");    stream.writeTextElement("title","Qt Home");    stream.writeEndElement();// bookmark    ...    stream.writeEndDocument();

QXmlStreamWriter takes care of prefixing namespaces, all you have to do is specify thenamespaceUri 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 eitherwriteNamespace() orwriteDefaultNamespace(). Alternatively, you can bypass the stream writer's namespace support and use overloaded methods that take a qualified name instead. The namespacehttp://www.w3.org/XML/1998/namespace is implicit and mapped to the prefixxml.

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 theautoFormatting property, and customized with theautoFormattingIndent property.

Other functions arewriteCDATA(),writeComment(),writeProcessingInstruction(), andwriteDTD(). Chaining of XML streams is supported withwriteCurrentToken().

By default,QXmlStreamWriter encodes XML in UTF-8. Different encodings can be enforced usingsetCodec().

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

TheQXmlStream Bookmarks Example illustrates how to use a stream writer to write an XML bookmark file (XBEL) that was previously read in by aQXmlStreamReader.

Property Documentation

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 theautoFormattingIndent property.

By default, auto-formatting is disabled.

This property was introduced in Qt 4.4.

Access functions:

boolautoFormatting() const
voidsetAutoFormatting(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.

The default indentation is 4.

This property was introduced in Qt 4.4.

Access functions:

intautoFormattingIndent() const
voidsetAutoFormattingIndent(int spacesOrTabs)

See alsoautoFormatting.

Member Function Documentation

QXmlStreamWriter::QXmlStreamWriter()

Constructs a stream writer.

See alsosetDevice().

QXmlStreamWriter::QXmlStreamWriter(QIODevice * device)

Constructs a stream writer that writes intodevice;

QXmlStreamWriter::QXmlStreamWriter(QByteArray * array)

Constructs a stream writer that writes intoarray. This is the same as creating an xml writer that operates on aQBuffer device which in turn operates onarray.

QXmlStreamWriter::QXmlStreamWriter(QString * string)

Constructs a stream writer that writes intostring.

QXmlStreamWriter::~QXmlStreamWriter()

Destructor.

QTextCodec * QXmlStreamWriter::codec() const

Returns the codec that is currently assigned to the stream.

See alsosetCodec().

QIODevice * QXmlStreamWriter::device() const

Returns the current device associated with theQXmlStreamWriter, or 0 if no device has been assigned.

See alsosetDevice().

bool QXmlStreamWriter::hasError() const

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.

This function was introduced in Qt 4.8.

void QXmlStreamWriter::setCodec(QTextCodec * codec)

Sets the codec for this stream tocodec. 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 callwriteStartDocument(). Call this function before callingwriteStartDocument().

See alsocodec().

void QXmlStreamWriter::setCodec(constchar * codecName)

Sets the codec for this stream to theQTextCodec for the encoding specified bycodecName. Common values forcodecName include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.

See alsoQTextCodec::codecForName().

void QXmlStreamWriter::setDevice(QIODevice * device)

Sets the current device todevice. If you want the stream to write into aQByteArray, you can create aQBuffer device.

See alsodevice().

void QXmlStreamWriter::writeAttribute(constQString & namespaceUri, constQString & name, constQString & value)

Writes an attribute withname andvalue, prefixed for the specifiednamespaceUri. If the namespace has not been declared yet,QXmlStreamWriter will generate a namespace declaration for it.

This function can only be called afterwriteStartElement() before any content is written, or afterwriteEmptyElement().

void QXmlStreamWriter::writeAttribute(constQString & qualifiedName, constQString & value)

This is an overloaded function.

Writes an attribute withqualifiedName andvalue.

This function can only be called afterwriteStartElement() before any content is written, or afterwriteEmptyElement().

void QXmlStreamWriter::writeAttribute(constQXmlStreamAttribute & attribute)

This is an overloaded function.

Writes theattribute.

This function can only be called afterwriteStartElement() before any content is written, or afterwriteEmptyElement().

void QXmlStreamWriter::writeAttributes(constQXmlStreamAttributes & attributes)

Writes the attribute vectorattributes. 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 afterwriteStartElement() before any content is written, or afterwriteEmptyElement().

See alsowriteAttribute() andwriteNamespace().

void QXmlStreamWriter::writeCDATA(constQString & text)

Writestext as CDATA section. Iftext 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, becausewriteCharacters() automatically escapes all non-content characters.

void QXmlStreamWriter::writeCharacters(constQString & text)

Writestext. The characters "<", "&", and """ are escaped as entity references "&lt;", "&amp;, and "&quot;". To avoid the forbidden sequence "]]>", ">" is also escaped as "&gt;".

See alsowriteEntityReference().

void QXmlStreamWriter::writeComment(constQString & text)

Writestext as XML comment, wheretext 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(constQXmlStreamReader & reader)

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

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

See alsoQXmlStreamReader::tokenType().

void QXmlStreamWriter::writeDTD(constQString & dtd)

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

void QXmlStreamWriter::writeDefaultNamespace(constQString & namespaceUri)

Writes a default namespace declaration fornamespaceUri.

IfwriteStartElement() orwriteEmptyElement() was called, the declaration applies to the current element; otherwise it applies to the next child element.

Note that the namespaceshttp://www.w3.org/XML/1998/namespace (bound toxmlns) andhttp://www.w3.org/2000/xmlns/ (bound toxml) by definition cannot be declared as default.

void QXmlStreamWriter::writeEmptyElement(constQString & namespaceUri, constQString & name)

Writes an empty element withname, prefixed for the specifiednamespaceUri. If the namespace has not been declared,QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls towriteAttribute() will add attributes to this element.

See alsowriteNamespace().

void QXmlStreamWriter::writeEmptyElement(constQString & qualifiedName)

This is an overloaded function.

Writes an empty element with qualified namequalifiedName. Subsequent calls towriteAttribute() will add attributes to this element.

void QXmlStreamWriter::writeEndDocument()

Closes all remaining open start elements and writes a newline.

See alsowriteStartDocument().

void QXmlStreamWriter::writeEndElement()

Closes the previous start element.

See alsowriteStartElement().

void QXmlStreamWriter::writeEntityReference(constQString & name)

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

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

Writes a namespace declaration fornamespaceUri withprefix. Ifprefix is empty,QXmlStreamWriter assigns a unique prefix consisting of the letter 'n' followed by a number.

IfwriteStartElement() orwriteEmptyElement() was called, the declaration applies to the current element; otherwise it applies to the next child element.

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

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

Writes an XML processing instruction withtarget anddata, wheredata must not contain the sequence "?>".

void QXmlStreamWriter::writeStartDocument(constQString & version)

Writes a document start with the XML version numberversion.

See alsowriteEndDocument().

void QXmlStreamWriter::writeStartDocument(constQString & version,bool standalone)

Writes a document start with the XML version numberversion and a standalone attributestandalone.

This function was introduced in Qt 4.5.

See alsowriteEndDocument().

void QXmlStreamWriter::writeStartDocument()

This is an overloaded function.

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

This function was introduced in Qt 4.5.

See alsowriteEndDocument() andsetCodec().

void QXmlStreamWriter::writeStartElement(constQString & namespaceUri, constQString & name)

Writes a start element withname, prefixed for the specifiednamespaceUri. If the namespace has not been declared yet,QXmlStreamWriter will generate a namespace declaration for it. Subsequent calls towriteAttribute() will add attributes to this element.

See alsowriteNamespace(),writeEndElement(), andwriteEmptyElement().

void QXmlStreamWriter::writeStartElement(constQString & qualifiedName)

This is an overloaded function.

Writes a start element withqualifiedName. Subsequent calls towriteAttribute() will add attributes to this element.

See alsowriteEndElement() andwriteEmptyElement().

void QXmlStreamWriter::writeTextElement(constQString & namespaceUri, constQString & name, constQString & text)

Writes a text element withname, prefixed for the specifiednamespaceUri, andtext. If the namespace has not been declared,QXmlStreamWriter will generate a namespace declaration for it.

This is a convenience function equivalent to:

void QXmlStreamWriter::writeTextElement(constQString & qualifiedName, constQString & text)

This is an overloaded function.

Writes a text element withqualifiedName andtext.

This is a convenience function equivalent to:

© 2016 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 theGNU 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.


[8]ページ先頭

©2009-2025 Movatter.jp