Procedural-style class to write out XML documents.
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:
AnewtXMLWriter instanceAnewtXMLWriter instanceAnewtXMLWriter::write_start_document()AnewtXMLWriter::write_start_element() and AnewtXMLWriter::write_attribute()AnewtXMLWriter::write_end_document()AnewtXMLWriter::render() or AnewtXMLWriter::flush()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.
__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.
$doctypeThe document type as string, or null to unset.
set_encoding($encoding) ¶Set the encoding for the generated document.
$encodingThe encoding as string, or null to unset.
set_content_type($content_type) ¶Set the content type for the generated document.
set_standalone($standalone) ¶Set whether the generated document is standalone.
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.
An AnewtXMLDomDocument instance.
Methods for writing elements to the document.
write_start_element($name, $attributes=null) ¶Write the start of an element.
$nameThe tag name for the element.
$attributesAssociative 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.
$nameThe 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.
$nameThe tag name for the element to write.
$dataThe textual data for this element.
$attributesAssociative 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_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.
$nameThe tag name for the element to write.
$dataThe textual data for this element.
$attributesAssociative array with initial attribute values for this element (optional). This parameter is for convenience only; you can always set attributes later using write_attribute.
Methods to write attributes to the currently open element.
write_attribute($name, $value) ¶Write an attribute to the currently open element.
$nameThe name of attribute for which to set the value.
$valueThe value of the attribute.
write_attributes($attributes) ¶Write multiple attributes to the currently open element.
Methods to write textual data to the document.
write_text($data) ¶Write textual to the currently open element.
Various methods to write data to the document.
write_comment($data) ¶Write a comment node to the document.
$dataThe comment text to write.
Methods to produce output.
flush() ¶Flush output to the browser.
This method renders the document and outputs it to the browser with the correct headers.
flush_to_file($filename) ¶Flush output to a file.
This method renders the document and outputs it to the specified file.
$filenameThe filename to write the output to.
$_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.