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

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

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:

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

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.

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

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.

(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:

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:

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: