Poco::XML

class XMLWriter

Library: XML
Package: XML
Header: Poco/XML/XMLWriter.h

Description

This class serializes SAX2 ContentHandler, LexicalHandler and DTDHandler events back into a stream.

Various consistency checks are performed on the written data (i.e. there must be exactly one root element and every startElement() must have a matching endElement()).

The XMLWriter supports optional pretty-printing of the serialized XML. Note, however, that pretty-printing XML data alters the information set of the document being written, since in XML all whitespace is potentially relevant to an application.

The writer contains extensive support for XML Namespaces, so that a client application does not have to keep track of prefixes and supply xmlns attributes.

If the client does not provide namespace prefixes (either by specifying them as part of the qualified name given to startElement(), or by calling startPrefixMapping()), the XMLWriter automatically generates namespace prefixes in the form ns1, ns2, etc.

Inheritance

Direct Base Classes: ContentHandler, LexicalHandler, DTDHandler

All Base Classes: ContentHandler, DTDHandler, LexicalHandler

Member Summary

Member Functions: addAttributes, addNamespaceAttributes, characters, closeStartTag, comment, dataElement, declareAttributeNamespaces, emptyElement, endCDATA, endDTD, endDocument, endElement, endEntity, endFragment, endPrefixMapping, getNewLine, ignorableWhitespace, nameToString, newPrefix, notationDecl, prettyPrint, processingInstruction, rawCharacters, setDocumentLocator, setNewLine, skippedEntity, startCDATA, startDTD, startDocument, startElement, startEntity, startFragment, startPrefixMapping, unparsedEntityDecl, writeAttributes, writeEndElement, writeIndent, writeMarkup, writeName, writeNewLine, writeStartElement, writeXML, writeXMLDeclaration

Inherited Functions: characters, comment, endCDATA, endDTD, endDocument, endElement, endEntity, endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, setDocumentLocator, skippedEntity, startCDATA, startDTD, startDocument, startElement, startEntity, startPrefixMapping, unparsedEntityDecl

Types

AttributeMap protected

typedef std::map < XMLString, XMLString > AttributeMap;

Enumerations

Options

CANONICAL = 0x00

do not write an XML declaration

WRITE_XML_DECLARATION = 0x01

write an XML declaration

PRETTY_PRINT = 0x02

pretty-print XML markup

Constructors

XMLWriter

XMLWriter(
    XMLByteOutputStream & str,
    int options
);

Creates the XMLWriter and sets the specified options.

The resulting stream will be UTF-8 encoded.

XMLWriter

XMLWriter(
    XMLByteOutputStream & str,
    int options,
    const std::string & encodingName,
    Poco::TextEncoding & textEncoding
);

Creates the XMLWriter and sets the specified options.

The encoding is reflected in the XML declaration. The caller is responsible for that the given encodingName matches with the given textEncoding.

XMLWriter

XMLWriter(
    XMLByteOutputStream & str,
    int options,
    const std::string & encodingName,
    Poco::TextEncoding * pTextEncoding
);

Creates the XMLWriter and sets the specified options.

The encoding is reflected in the XML declaration. The caller is responsible for that the given encodingName matches with the given textEncoding. If pTextEncoding is null, the given encodingName is ignored and the default UTF-8 encoding is used.

Destructor

~XMLWriter virtual

~XMLWriter();

Destroys the XMLWriter.

Member Functions

characters virtual

void characters(
    const XMLChar ch[],
    int start,
    int length
);

Writes XML character data. Quotes, ampersand's, less-than and greater-than signs are escaped, unless a CDATA section has been opened by calling startCDATA().

The characters must be encoded in UTF-8 (if XMLChar is char) or UTF-16 (if XMLChar is wchar_t).

See also: Poco::XML::ContentHandler::characters()

characters

void characters(
    const XMLString & str
);

Writes XML character data. Quotes, ampersand's, less-than and greater-than signs are escaped, unless a CDATA section has been opened by calling startCDATA().

The characters must be encoded in UTF-8 (if XMLChar is char) or UTF-16 (if XMLChar is wchar_t).

comment virtual

void comment(
    const XMLChar ch[],
    int start,
    int length
);

Writes a comment.

See also: Poco::XML::LexicalHandler::comment()

dataElement

void dataElement(
    const XMLString & namespaceURI,
    const XMLString & localName,
    const XMLString & qname,
    const XMLString & data,
    const XMLString & attr1 = XMLString (),
    const XMLString & value1 = XMLString (),
    const XMLString & attr2 = XMLString (),
    const XMLString & value2 = XMLString (),
    const XMLString & attr3 = XMLString (),
    const XMLString & value3 = XMLString ()
);

Writes a data element in the form <name attr1="value1"...>data</name>.

emptyElement

void emptyElement(
    const XMLString & namespaceURI,
    const XMLString & localName,
    const XMLString & qname
);

Writes an empty XML element tag (<elem/>).

emptyElement

void emptyElement(
    const XMLString & namespaceURI,
    const XMLString & localName,
    const XMLString & qname,
    const Attributes & attributes
);

Writes an empty XML element tag with the given attributes (<elem attr1="value1"... />).

endCDATA virtual

void endCDATA();

Writes the ] string that ends a CDATA section.

See also: Poco::XML::LexicalHandler::endCDATA()

endDTD virtual

void endDTD();

Writes the closing characters of a DTD declaration.

See also: Poco::XML::LexicalHandler::endDTD()

endDocument virtual

void endDocument();

Checks that all elements are closed and prints a final newline.

See also: Poco::XML::ContentHandler::endDocument()

endElement virtual

void endElement(
    const XMLString & namespaceURI,
    const XMLString & localName,
    const XMLString & qname
);

Writes an XML end element tag.

Throws an exception if the name of doesn't match the one of the most recent startElement().

See also: Poco::XML::ContentHandler::endElement()

endEntity virtual

void endEntity(
    const XMLString & name
);

Does nothing.

See also: Poco::XML::LexicalHandler::endEntity()

endFragment

void endFragment();

Checks that all elements are closed and prints a final newline.

endPrefixMapping virtual

void endPrefixMapping(
    const XMLString & prefix
);

End the scope of a prefix-URI mapping.

See also: Poco::XML::ContentHandler::endPrefixMapping()

getNewLine

const std::string & getNewLine() const;

Returns the line ending currently in use.

ignorableWhitespace virtual

void ignorableWhitespace(
    const XMLChar ch[],
    int start,
    int length
);

Writes whitespace characters by simply passing them to characters().

See also: Poco::XML::ContentHandler::ignorableWhitespace()

notationDecl virtual

void notationDecl(
    const XMLString & name,
    const XMLString * publicId,
    const XMLString * systemId
);

See also: Poco::XML::DTDHandler::notationDecl()

processingInstruction virtual

void processingInstruction(
    const XMLString & target,
    const XMLString & data
);

Writes a processing instruction.

See also: Poco::XML::ContentHandler::processingInstruction()

rawCharacters

void rawCharacters(
    const XMLString & str
);

Writes the characters in the given string as they are. The caller is responsible for escaping characters as necessary to produce valid XML.

The characters must be encoded in UTF-8 (if XMLChar is char) or UTF-16 (if XMLChar is wchar_t).

setDocumentLocator virtual

void setDocumentLocator(
    const Locator * loc
);

Currently unused.

See also: Poco::XML::ContentHandler::setDocumentLocator()

setNewLine

void setNewLine(
    const std::string & newLineCharacters
);

Sets the line ending for the resulting XML file.

Possible values are: