December 30, 2014 8:32 am

Writing to XML files in PHP using XMLWriter and DOM

Using PHP’s XMLWriter to write XML files is not very complicated. It uses element nesting to create the tree you want and it saves the tree to the memory so you can output the contents of the memory and the tree in any place you want, be it a file or display to the browser. You can easily create dynamic XML files by fetching some data from a database and using XMLWriter. XMLWriter is preferred to DOMDocument when working with larger files because you can periodically flush the XML in memory to a file and keep building the tree you want without running out of memory.

Writing to XML files in PHP using XMLWriter and DOM

[wpdm_file id=121]

Firstly, we need to create a XMLWriter object, start writing to the machine’s memory and begin a document.

$xml=new XMLWriter();
$xml->startDocument('1.0', 'UTF-8');

Then, we have to create our main element that will contain all child nodes.


We use startElement(“element-name”) to start an element with a given name and we can nest all child elements in it which can have attributes, own children and text inside.

Then, we create our article element (In a real document there would be many “article”, “product”, etc. elements that would contain children with all relevant data about that product, article or whatever the xml tree depicts.


To add an attribute to a started element we use:

$xml->writeAttribute("id", 1) ($xml->writeAttribute("attribute-key", attribute-value);

We create a child of the article with id 1 called “published” and add text to the tag giving the current date and time:

$xml->text(date("d/m/Y h:i:s"));

To end an element we use:


This ends the most recently started element (“published”).

We create another child of article with id 1 with an attribute and text.

$xml->writeAttribute("position", "writer");
$xml->text("John Stewart");

Then we close all elements and write the contents of the memory to a file called ‘articles.xml’.

file_put_contents('articles.xml', $xml->outputMemory());

To flush a file and free memory you can use the following snippet and flush the XML that is in memory out to the file in order to free it.

file_put_contents('articles.xml', $xml->flush(true), FILE_APPEND);

(Append to the file articles.xml and flush the used memory so far)

Those are the basics of XMLWriter. XMLWriter is best suited for writing xml files from scratch and not for editing already existing xml files.

To append to an existing file, we have used DOM and have provided you with a quick tutorial as well. If you know some JavaScript then the syntax would be pretty familiar to you and you will see how easy it is to work with with DOM.

We create a DOM object by:

$dom=new DOMDocument();

Then we load a file:


We use createElement(‘el-name’) to create an element, setAttribute(‘attrkey’, ‘attrvalue’) to set attribute of an element, createTextNode(‘text’) to create a text that we have to append to an element and appendChild($elOrText) to append a child element or text to a preexisting element.

After we have finished we append all the main nodes we have added to the documentElement and save the file. Here is a demo of how we have added another article element to the file we created with XMLWriter:

$dom=new DOMDocument();


$article->setAttribute("id", 2);
$publishedDate=$dom->createTextNode(date("d/m/Y h:i:s", strtotime("tomorrow")));
$author->setAttribute("position", "writer");
$authorName=$dom->createTextNode("Ivan Dimov");


Now, you can start using XMLWriter and DOM to build xml files for variety of purposes and learn more about the topic.

Author Ivan Dimov

Ivan is a student of IT, a freelance web designer/developer and a tech writer. He deals with both front-end and back-end stuff. Whenever he is not in front of an Internet-enabled device he is probably reading a book or traveling. You can find more about him at: facebook, twitter

Tutorial Categories:

Leave a Reply

Your email address will not be published. Required fields are marked *