AnewtXMLWriter class

Procedural-style class to write out XML documents.

Class Overview

Procedural-style class to write out XML documents.

This class can be used to write out XML documents with a simple procedural interface, allowing you to build output documents in a linear way. The basic usage pattern is:

Note that this class operates completely using in-memory DOM tree representations. It is not suitable to write very large XML documents to disk, but for relatively small XML documents it should be good enough.

Public Methods

__construct()

Create a new XML writer instance.

These methods should be used to start and end documents. Calls to AnewtXMLWriter::write_start_document() and AnewtXMLWriter::write_end_document() should appear at the start and the end of your code, respectively.

write_start_document()

Write the start of the document.

write_end_document()

Write the end of the document.

After calling this method, no further data can be written to this AnewtXMLWriter instance.

set_document_type($doctype)

Set the document type for the generated document.

Parameters

$doctype

The document type as string, or null to unset.

See also

set_encoding($encoding)

Set the encoding for the generated document.

Parameters

$encoding

The encoding as string, or null to unset.

See also

set_content_type($content_type)

Set the content type for the generated document.

Parameters

$content_type

The content type as string.

See also

set_standalone($standalone)

Set whether the generated document is standalone.

Parameters

$standalone

Boolean or null to unset.

See also

get_document()

Return the AnewtXMLDomDocument instance this AnewtXMLWriter uses.

This method can be used to retrieve the generated document. This method is only supposed to be called after a call to AnewtXMLWriter::write_end_document(). Usually you don't need this method, since the AnewtXMLWriter::render() and AnewtXMLWriter::flush() methods can take care of rendering the document properly.

Return value

An AnewtXMLDomDocument instance.

Element methods

Methods for writing elements to the document.

write_start_element($name, $attributes=null)

Write the start of an element.

Parameters

$name

The tag name for the element.

$attributes

Associative array with initial attribute values for this element (optional). This parameter is for convenience only; you can always set attributes later using write_attribute.

write_end_element($name=null)

Write the end of an element.

Parameters

$name

The tag name of the element to end (optional). This value is not strictly needed, but if specified it is tested against the currently open element, triggering an assertion error if the wrong elements was open. This is mainly helpful to make sure your code is correct and you don't close elements you did not expect to close.

write_element_text($name, $data, $attributes=null)

Write element with textual data.

This method opens a new element, writes the text and ends the element again.

Parameters

$name

The tag name for the element to write.

$data

The textual data for this element.

$attributes

Associative array with initial attribute values for this element (optional). This parameter is for convenience only; you can always set attributes later using write_attribute.

See also

write_element_raw($name, $data, $attributes=null)

Write element with raw data.

This method opens a new element, writes the raw data and ends the element again.

Parameters

$name

The tag name for the element to write.

$data

The textual data for this element.

$attributes

Associative array with initial attribute values for this element (optional). This parameter is for convenience only; you can always set attributes later using write_attribute.

See also

Attribute methods

Methods to write attributes to the currently open element.

write_attribute($name, $value)

Write an attribute to the currently open element.

Parameters

$name

The name of attribute for which to set the value.

$value

The value of the attribute.

See also

write_attributes($attributes)

Write multiple attributes to the currently open element.

Parameters

$attributes

Associative array with attribute named and values.

See also

Textual data methods

Methods to write textual data to the document.

write_text($data)

Write textual to the currently open element.

Parameters

$data

The textual data for this element.

See also

write_raw($data)

Write raw data to the currently open element.

Parameters

$data

The raw data for this element.

See also

Miscellaneous writer methods

Various methods to write data to the document.

write_comment($data)

Write a comment node to the document.

Parameters

$data

The comment text to write.

Output methods

Methods to produce output.

render()

Render this document to a string.

Return value

Rendered string with XML data.

See also

flush()

Flush output to the browser.

This method renders the document and outputs it to the browser with the correct headers.

See also

flush_to_file($filename)

Flush output to a file.

This method renders the document and outputs it to the specified file.

Parameters

$filename

The filename to write the output to.

See also

Private Attributes

$_document [private]

The document used in this AnewtXMLWriter instance.

See also

$_current_element [private]

Reference to the current element.

This points to the variable to which subsequent calls to the write methods will add child nodes, attributes or textual content.