Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : XMLWriter Functions

XMLWriter Functions


This is the XMLWriter extension. It wraps the libxml xmlWriter API.

This extension represents a writer that provides a non-cached, forward-only means of generating streams or files containing XML data.

This extension can be used in an object oriented style or a procedural one. Every method documented describes the alternative procedural call.

Predefined Classes



Resource Types

There is one resource used by the procedural version of the XMLWriter extension: returned by xmlwriter_open_memory() or xmlwriter_open_uri().

Predefined Constants

This extension has no constants defined.

Table of Contents

XMLWriter::endAttribute — End attribute
XMLWriter::endCData — End current CDATA
XMLWriter::endComment — Create end comment
XMLWriter::endDocument — End current document
XMLWriter::endDTDAttlist — End current DTD AttList
XMLWriter::endDTDElement — End current DTD element
XMLWriter::endDTDEntity — End current DTD Entity
XMLWriter::endDTD — End current DTD
XMLWriter::endElement — End current element
XMLWriter::endPI — End current PI
XMLWriter::flush — Flush current buffer
XMLWriter::fullEndElement — End current element
XMLWriter::openMemory — Create new xmlwriter using memory for string output
XMLWriter::openURI — Create new xmlwriter using source uri for output
XMLWriter::outputMemory — Returns current buffer
XMLWriter::setIndentString — Set string used for indenting
XMLWriter::setIndent — Toggle indentation on/off
XMLWriter::startAttributeNS — Create start namespaced attribute
XMLWriter::startAttribute — Create start attribute
XMLWriter::startCData — Create start CDATA tag
XMLWriter::startComment — Create start comment
XMLWriter::startDocument — Create document tag
XMLWriter::startDTDAttlist — Create start DTD AttList
XMLWriter::startDTDElement — Create start DTD element
XMLWriter::startDTDEntity — Create start DTD Entity
XMLWriter::startDTD — Create start DTD tag
XMLWriter::startElementNS — Create start namespaced element tag
XMLWriter::startElement — Create start element tag
XMLWriter::startPI — Create start PI tag
XMLWriter::text — Write text
XMLWriter::writeAttributeNS — Write full namespaced attribute
XMLWriter::writeAttribute — Write full attribute
XMLWriter::writeCData — Write full CDATA tag
XMLWriter::writeComment — Write full comment tag
XMLWriter::writeDTDAttlist — Write full DTD AttList tag
XMLWriter::writeDTDElement — Write full DTD element tag
XMLWriter::writeDTDEntity — Write full DTD Entity tag
XMLWriter::writeDTD — Write full DTD tag
XMLWriter::writeElementNS — Write full namesapced element tag
XMLWriter::writeElement — Write full element tag
XMLWriter::writePI — Writes a PI
XMLWriter::writeRaw — Write a raw XML text

Code Examples / Notes » ref.xmlwriter

carlos averett

Using XMLWriter to create a WAP page:
$memory = xmlwriter_open_memory();
xmlwriter_start_dtd($memory,'html','-//WAPFORUM//DTD XHTML Mobile 1.0//EN', '');
xmlwriter_start_element ($memory,'html'); // <html>
xmlwriter_write_attribute( $memory, 'xmlns', '');
xmlwriter_write_attribute( $memory, 'xm:lang', 'en');
xmlwriter_start_element($memory,'head'); // <head>
xmlwriter_write_element ($memory,'title', 'Test WAP Document');
xmlwriter_end_element($memory); // </head>
xmlwriter_start_element($memory,'body'); // <body>
xmlwriter_start_element($memory,'ol'); // <ol>
xmlwriter_write_element ($memory,'li', 'One Item');
xmlwriter_write_element ($memory,'li', 'Another Item');
xmlwriter_write_element ($memory,'li', 'Another Item');
xmlwriter_end_element($memory); // </ol>
xmlwriter_end_element($memory); // </body>
xmlwriter_end_element($memory); // </html>
$xml = xmlwriter_output_memory($memory,true);
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "">
<html xmlns="" xm:lang="en">
<title>Test WAP Document</title>
<li>One Item</li>
<li>Another Item</li>
<li>Another Item</li>

yves sucaet

If you want your XML-output to be seen as XML by the browser, you need to modify your header. The XmlWriter does not do this for you! Therefore, the first line of your script should be:
header("Content-type: text/xml");

alexandre arica

How to generate a simple XML document for a XSL-Transformation purpose.
We have 3 files:
- 'index.php' : output a XML document
- 'XmlConstruct.class.php' : allow to construct a XML document with 'xmlwriter'
- 'index.xsl' : contains a XSLT document
Contents of the file 'index.php' :
$contents = array('page_title' => 'Generate a XHTML page from XML+XSLT files',
'welcome_msg' => 'Simple XHTML document from XML+XSLT files!');
$XmlConstruct = new XmlConstruct('rootElement', 'index.xsl');
Contents of the file  'XmlConstruct.class.php' :
* Construct a simple XML document.
* This class inherits from the (PHP) class 'xmlwriter'.
* You will need at least PHP 5.1.2
* @author Alexandre Arica
* @since  16 April 2006
* @version 1.0 modified the 16 April 2006
class XmlConstruct extends XMLWriter
* Constructor.
* @param string $prm_rootElementName A root element's name of a current xml document
* @param string $prm_xsltFilePath Path of a XSLT file.
* @access public
* @param null
public function __construct($prm_rootElementName, $prm_xsltFilePath=''){
$this->setIndentString(' ');
$this->startDocument('1.0', 'UTF-8');
$this->writePi('xml-stylesheet', 'type="text/xsl" href="'.$prm_xsltFilePath.'"');
* Set an element with a text to a current xml document.
* @access public
* @param string $prm_elementName An element's name
* @param string $prm_ElementText An element's text
* @return null
public function setElement($prm_elementName, $prm_ElementText){
* Construct elements and texts from an array.
* The array should contain an attribute's name in index part
* and a attribute's text in value part.
* @access public
* @param array $prm_array Contains attributes and texts
* @return null
public function fromArray($prm_array){
foreach ($prm_array as $index => $text){
$this->setElement($index, $text);
* Return the content of a current xml document.
* @access public
* @param null
* @return string Xml document
public function getDocument(){
return $this->outputMemory();
* Output the content of a current xml document.
* @access public
* @param null
public function output(){
header('Content-type: text/xml');
echo $this->getDocument();

Contents of the file 'index.xsl' :
<?xml version="1.0" encoding="utf-8"?>
<xsl:template match="rootElement">
<html xmlns="">
<title><xsl:value-of select="page_title" /></title>

<xsl:value-of select="welcome_msg" />


robajz a robajz . info

There already is a class xmlWriter, although it is not documented. This is a rewrite of the example from [ Carlos Averett] and works well on my php 5.1.6
   $xw = new xmlWriter();
   $xw->startDtd('html','-//WAPFORUM//DTD XHTML Mobile 1.0//EN', '');
   $xw->startElement ('html'); // <html>
   $xw->writeAttribute( 'xmlns', '');
   $xw->writeAttribute( 'xm:lang', 'en');
   $xw->startElement('head'); // <head>
   $xw->writeElement ('title', 'Test WAP Document');
   $xw->endElement(); // </head>
   $xw->startElement('body'); // <body>
   $xw->startElement('ol'); // <ol>
   $xw->writeElement ('li', 'One Item &amp;  <sss <ss />></ss>');
   $xw->writeElement ('li', 'Another Item');
   $xw->writeElement ('li', 'Another Item');
   $xw->endElement(); // </ol>
   $xw->endElement(); // </body>
   $xw->endElement(); // </html>
   print $xw->outputMemory(true);    

Change Language

Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Haru PDF Functions
hash Functions
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker