Qt 4.8
|
The QXmlStreamWriter class provides an XML writer with a simple streaming API. More...
#include <qxmlstream.h>
Public Functions | |
bool | autoFormatting () const |
This property controls whether or not the stream writer automatically formats the generated XML data. More... | |
int | autoFormattingIndent () const |
the number of spaces or tabs used for indentation when auto-formatting is enabled. More... | |
QTextCodec * | codec () const |
Returns the codec that is currently assigned to the stream. More... | |
QIODevice * | device () const |
Returns the current device associated with the QXmlStreamWriter, or 0 if no device has been assigned. More... | |
bool | hasError () const |
Returns true if the stream failed to write to the underlying device; otherwise returns false. More... | |
QXmlStreamWriter () | |
Constructs a stream writer. More... | |
QXmlStreamWriter (QIODevice *device) | |
Constructs a stream writer that writes into device;. More... | |
QXmlStreamWriter (QByteArray *array) | |
Constructs a stream writer that writes into array. More... | |
QXmlStreamWriter (QString *string) | |
Constructs a stream writer that writes into string. More... | |
void | setAutoFormatting (bool) |
void | setAutoFormattingIndent (int spacesOrTabs) |
void | setCodec (QTextCodec *codec) |
Sets the codec for this stream to codec. More... | |
void | setCodec (const char *codecName) |
Sets the codec for this stream to the QTextCodec for the encoding specified by codecName. More... | |
void | setDevice (QIODevice *device) |
Sets the current device to device. More... | |
void | writeAttribute (const QString &qualifiedName, const QString &value) |
Writes an attribute with qualifiedName and value. More... | |
void | writeAttribute (const QString &namespaceUri, const QString &name, const QString &value) |
Writes an attribute with name and value, prefixed for the specified namespaceUri. More... | |
void | writeAttribute (const QXmlStreamAttribute &attribute) |
Writes the attribute. More... | |
void | writeAttributes (const QXmlStreamAttributes &attributes) |
Writes the attribute vector attributes. More... | |
void | writeCDATA (const QString &text) |
Writes text as CDATA section. More... | |
void | writeCharacters (const QString &text) |
Writes text. More... | |
void | writeComment (const QString &text) |
Writes text as XML comment, where text must not contain the forbidden sequence "--" or end with "-". More... | |
void | writeCurrentToken (const QXmlStreamReader &reader) |
Writes the current state of the reader. More... | |
void | writeDefaultNamespace (const QString &namespaceUri) |
Writes a default namespace declaration for namespaceUri. More... | |
void | writeDTD (const QString &dtd) |
Writes a DTD section. More... | |
void | writeEmptyElement (const QString &qualifiedName) |
Writes an empty element with qualified name qualifiedName. More... | |
void | writeEmptyElement (const QString &namespaceUri, const QString &name) |
Writes an empty element with name, prefixed for the specified namespaceUri. More... | |
void | writeEndDocument () |
Closes all remaining open start elements and writes a newline. More... | |
void | writeEndElement () |
Closes the previous start element. More... | |
void | writeEntityReference (const QString &name) |
Writes the entity reference name to the stream, as "&\a{name};". More... | |
void | writeNamespace (const QString &namespaceUri, const QString &prefix=QString()) |
Writes a namespace declaration for namespaceUri with prefix. More... | |
void | writeProcessingInstruction (const QString &target, const QString &data=QString()) |
Writes an XML processing instruction with target and data, where data must not contain the sequence "?>". More... | |
void | writeStartDocument () |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Writes a document start with XML version number "1.0". More... | |
void | writeStartDocument (const QString &version) |
Writes a document start with the XML version number version. More... | |
void | writeStartDocument (const QString &version, bool standalone) |
Writes a document start with the XML version number version and a standalone attribute standalone. More... | |
void | writeStartElement (const QString &qualifiedName) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Writes a start element with qualifiedName. More... | |
void | writeStartElement (const QString &namespaceUri, const QString &name) |
Writes a start element with name, prefixed for the specified namespaceUri. More... | |
void | writeTextElement (const QString &qualifiedName, const QString &text) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes a text element with qualifiedName and text. More... | |
void | writeTextElement (const QString &namespaceUri, const QString &name, const QString &text) |
Writes a text element with name, prefixed for the specified namespaceUri, and text. More... | |
~QXmlStreamWriter () | |
Destructor. More... | |
Properties | |
QScopedPointer< QXmlStreamWriterPrivate > | d_ptr |
The QXmlStreamWriter class provides an XML writer with a simple streaming API.
QXmlStreamWriter is the counterpart to QXmlStreamReader for writing XML. Like its related class, it operates on a QIODevice specified with setDevice(). 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 with writeStartDocument() and end it with writeEndDocument(). This will implicitly close all remaining open tags.
Element tags are opened with writeStartElement() followed by writeAttribute() or 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 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() or 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 property.
Other functions are writeCDATA(), writeComment(), writeProcessingInstruction(), and writeDTD(). Chaining of XML streams is supported with writeCurrentToken().
By default, 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 Bookmarks Example illustrates how to use a stream writer to write an XML bookmark file (XBEL) that was previously read in by a QXmlStreamReader.
Definition at line 416 of file qxmlstream.h.
QXmlStreamWriter::QXmlStreamWriter | ( | ) |
Constructs a stream writer.
Definition at line 3368 of file qxmlstream.cpp.
QXmlStreamWriter::QXmlStreamWriter | ( | QIODevice * | device | ) |
Constructs a stream writer that writes into device;.
Definition at line 3376 of file qxmlstream.cpp.
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.
Definition at line 3387 of file qxmlstream.cpp.
QXmlStreamWriter::QXmlStreamWriter | ( | QString * | string | ) |
Constructs a stream writer that writes into string.
Definition at line 3399 of file qxmlstream.cpp.
QXmlStreamWriter::~QXmlStreamWriter | ( | ) |
bool QXmlStreamWriter::autoFormatting | ( | ) | const |
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 property.
By default, auto-formatting is disabled.
Returns true
if auto formattting is enabled, otherwise false
.
Definition at line 3532 of file qxmlstream.cpp.
Referenced by QXmlStreamWriterPrivate::writeStartElement().
int QXmlStreamWriter::autoFormattingIndent | ( | ) | const |
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.
Definition at line 3561 of file qxmlstream.cpp.
QTextCodec * QXmlStreamWriter::codec | ( | ) | const |
Returns the codec that is currently assigned to the stream.
Definition at line 3487 of file qxmlstream.cpp.
Referenced by setCodec().
QIODevice * QXmlStreamWriter::device | ( | ) | const |
Returns the current device associated with the QXmlStreamWriter, or 0 if no device has been assigned.
Definition at line 3439 of file qxmlstream.cpp.
Referenced by QXmlStreamWriter(), and setDevice().
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.
Definition at line 3579 of file qxmlstream.cpp.
void QXmlStreamWriter::setAutoFormatting | ( | bool | enable | ) |
Enables auto formatting if enable is true
, otherwise disables it.
The default value is false
.
Definition at line 3521 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll().
void QXmlStreamWriter::setAutoFormattingIndent | ( | int | spacesOrTabs | ) |
Definition at line 3555 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll().
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().
Definition at line 3458 of file qxmlstream.cpp.
Referenced by setCodec(), and QTextOdfWriter::writeAll().
void QXmlStreamWriter::setCodec | ( | const char * | codecName | ) |
Sets the codec for this stream to the QTextCodec for the encoding specified by codecName.
Common values for codecName
include "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't recognized, nothing happens.
Definition at line 3477 of file qxmlstream.cpp.
void QXmlStreamWriter::setDevice | ( | QIODevice * | device | ) |
Sets the current device to device.
If you want the stream to write into a QByteArray, you can create a QBuffer device.
Definition at line 3420 of file qxmlstream.cpp.
Writes an attribute with qualifiedName and value.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function can only be called after writeStartElement() before any content is written, or after writeEmptyElement().
Definition at line 3596 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll(), writeAttribute(), writeAttributes(), QTextOdfWriter::writeBlock(), QTextOdfWriter::writeBlockFormat(), QTextOdfWriter::writeCharacterFormat(), QTextOdfWriter::writeFrame(), QTextOdfWriter::writeFrameFormat(), QTextOdfWriter::writeInlineCharacter(), QTextOdfWriter::writeListFormat(), and QTextOdfWriter::writeTableCellFormat().
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().
Definition at line 3616 of file qxmlstream.cpp.
void QXmlStreamWriter::writeAttribute | ( | const QXmlStreamAttribute & | attribute | ) |
Writes the attribute.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function can only be called after writeStartElement() before any content is written, or after writeEmptyElement().
Definition at line 3644 of file qxmlstream.cpp.
void QXmlStreamWriter::writeAttributes | ( | const QXmlStreamAttributes & | attributes | ) |
Writes the attribute vector attributes.
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().
Definition at line 3665 of file qxmlstream.cpp.
Referenced by writeCurrentToken().
void QXmlStreamWriter::writeCDATA | ( | const QString & | text | ) |
Writes 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.
Definition at line 3683 of file qxmlstream.cpp.
Referenced by writeCurrentToken().
void QXmlStreamWriter::writeCharacters | ( | const QString & | text | ) |
Writes text.
The characters "<", "&", and "\"" are escaped as entity references "<", "&, and """. To avoid the forbidden sequence "]]>", ">" is also escaped as ">".
Definition at line 3701 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeBlock(), writeCurrentToken(), and writeTextElement().
void QXmlStreamWriter::writeComment | ( | const QString & | text | ) |
Writes 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.
Definition at line 3713 of file qxmlstream.cpp.
Referenced by writeCurrentToken().
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.
Definition at line 4071 of file qxmlstream.cpp.
void QXmlStreamWriter::writeDefaultNamespace | ( | const QString & | namespaceUri | ) |
Writes a default namespace declaration for namespaceUri.
If writeStartElement() or 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.
Definition at line 3915 of file qxmlstream.cpp.
void QXmlStreamWriter::writeDTD | ( | const QString & | dtd | ) |
Writes a DTD section.
The dtd represents the entire doctypedecl production from the XML 1.0 specification.
Definition at line 3729 of file qxmlstream.cpp.
Referenced by writeCurrentToken().
void QXmlStreamWriter::writeEmptyElement | ( | const QString & | qualifiedName | ) |
Writes an empty element with qualified name qualifiedName.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Subsequent calls to writeAttribute() will add attributes to this element.
Definition at line 3749 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeBlock(), QTextOdfWriter::writeBlockFormat(), QTextOdfWriter::writeCharacterFormat(), QTextOdfWriter::writeFrame(), QTextOdfWriter::writeFrameFormat(), QTextOdfWriter::writeListFormat(), and QTextOdfWriter::writeTableCellFormat().
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.
Definition at line 3765 of file qxmlstream.cpp.
void QXmlStreamWriter::writeEndDocument | ( | ) |
Closes all remaining open start elements and writes a newline.
Definition at line 3812 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll(), and writeCurrentToken().
void QXmlStreamWriter::writeEndElement | ( | ) |
Closes the previous start element.
Definition at line 3825 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll(), QTextOdfWriter::writeBlock(), QTextOdfWriter::writeBlockFormat(), QTextOdfWriter::writeCharacterFormat(), writeCurrentToken(), writeEndDocument(), QTextOdfWriter::writeFormats(), QTextOdfWriter::writeFrame(), QTextOdfWriter::writeFrameFormat(), QTextOdfWriter::writeInlineCharacter(), QTextOdfWriter::writeListFormat(), QTextOdfWriter::writeTableCellFormat(), and writeTextElement().
void QXmlStreamWriter::writeEntityReference | ( | const QString & | name | ) |
Writes the entity reference name to the stream, as "&\a{name};".
Definition at line 3861 of file qxmlstream.cpp.
Referenced by writeCurrentToken().
void QXmlStreamWriter::writeNamespace | ( | const QString & | namespaceUri, |
const QString & | prefix = QString() |
||
) |
Writes a namespace declaration for namespaceUri with prefix.
If prefix is empty, QXmlStreamWriter assigns a unique prefix consisting of the letter 'n' followed by a number.
If writeStartElement() or 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.
Definition at line 3886 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll(), and writeCurrentToken().
void QXmlStreamWriter::writeProcessingInstruction | ( | const QString & | target, |
const QString & | data = QString() |
||
) |
Writes an XML processing instruction with target and data, where data must not contain the sequence "?>".
Definition at line 3932 of file qxmlstream.cpp.
Referenced by writeCurrentToken().
void QXmlStreamWriter::writeStartDocument | ( | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Writes a document start with XML version number "1.0".
This also writes the encoding information.
Definition at line 3957 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll(), and writeCurrentToken().
void QXmlStreamWriter::writeStartDocument | ( | const QString & | version | ) |
Writes a document start with the XML version number version.
Definition at line 3968 of file qxmlstream.cpp.
void QXmlStreamWriter::writeStartDocument | ( | const QString & | version, |
bool | standalone | ||
) |
Writes a document start with the XML version number version and a standalone attribute standalone.
Definition at line 3991 of file qxmlstream.cpp.
void QXmlStreamWriter::writeStartElement | ( | const QString & | qualifiedName | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.Writes a start element with qualifiedName.
Subsequent calls to writeAttribute() will add attributes to this element.
Definition at line 4019 of file qxmlstream.cpp.
Referenced by QTextOdfWriter::writeAll(), QTextOdfWriter::writeBlock(), QTextOdfWriter::writeBlockFormat(), QTextOdfWriter::writeCharacterFormat(), writeCurrentToken(), QTextOdfWriter::writeFormats(), QTextOdfWriter::writeFrame(), QTextOdfWriter::writeFrameFormat(), QTextOdfWriter::writeInlineCharacter(), QTextOdfWriter::writeListFormat(), QTextOdfWriter::writeTableCellFormat(), and writeTextElement().
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.
Definition at line 4035 of file qxmlstream.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Writes a text element with qualifiedName and text.
This is a convenience function equivalent to:
Definition at line 3782 of file qxmlstream.cpp.
void QXmlStreamWriter::writeTextElement | ( | const QString & | namespaceUri, |
const QString & | name, | ||
const QString & | text | ||
) |
Writes a text element with name, prefixed for the specified namespaceUri, and text.
If the namespace has not been declared, QXmlStreamWriter will generate a namespace declaration for it.
This is a convenience function equivalent to:
Definition at line 3799 of file qxmlstream.cpp.
|
private |
Definition at line 482 of file qxmlstream.h.