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.
Firstly, we need to create a XMLWriter object, start writing to the machine’s memory and begin a document.
$xml=new XMLWriter(); $xml->openMemory(); $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->startElement("published"); $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->startElement("author"); $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’.
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.
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:
$dom=new DOMDocument(); $dom->load("articles.xml"); $article=$dom->createElement("article"); $article->setAttribute("id", 2); $published=$dom->CreateElement("published"); $publishedDate=$dom->createTextNode(date("d/m/Y h:i:s", strtotime("tomorrow"))); $published->appendChild($publishedDate); $article->appendChild($published); $author=$dom->createElement("author"); $author->setAttribute("position", "writer"); $authorName=$dom->createTextNode("Ivan Dimov"); $author->appendChild($authorName); $article->appendChild($author); $dom->documentElement->appendChild($article); $dom->save("articles.xml");
Now, you can start using XMLWriter and DOM to build xml files for variety of purposes and learn more about the topic.