Recently, I made an XML sitemap for my blog. I created a PHP script that will generate an updated XML sitemap whenever I create a new post. So I thought I would create a tutorial on how to generate an XML file dynamically and manipulate it, and that is what we will discuss in this tutorial.
Without further delay, let's get started.
-
Set up the DOMDocument object
To manipulate an XML in PHP, there is a DOMDocument object that we can use.
$dom = new DOMDocument();
We can set the encoding and the version of the XML using the DOMDocument encoding and xmlVersion properties.
$dom->encoding = 'utf-8'; $dom->xmlVersion = '1.0';
If you want the XML output to have proper indentation and spaces, set the formatOutput property of DOMDocument to true.
$dom->formatOutput = true;
-
Add an Element and Attribute
To create an element, the DOMDocument has the createElement() method, which creates a DOMElement object. The method takes two parameters: the first parameter is the tagname, and the second parameter is the value of the element. Omitting the second parameter will create an empty element.
$root = $dom->createElement('tagname', 'value');
To add an attribute to an element, we can use the setAttribute() method of the DOMElement object. Just like createElement(), this method takes two parameters: the first is the name of the attribute, and the second is the value of the attribute.
$root->setAttribute('attribute-name', 'value');
The element created by the createElement() method will not automatically appear in the document. To make it appear in the document, we need to use the appendChild() method. We can use this method both in DOMDocument and DOMElement objects. This method takes one parameter, which is the element that we will append.
$dom->appendChild($root);
-
Save the XML
Finally, after we are done manipulating our XML, we can now save it using the save() method of DOMDocument. This method takes the XML filename as the first parameter.
$dom->save("sample.xml");
-
Example of Creating an XML
Code:
<?php $dom = new DOMDocument(); $dom->encoding = 'utf-8'; $dom->xmlVersion = '1.0'; $dom->formatOutput = true; $root = $dom->createElement('conversation'); $element = $dom->createElement('message', 'Hi, How are you.'); $element->setAttribute('sender', 'John'); $root->appendChild($element); $dom->appendChild($root); $dom->save("sample.xml");
Output:
<?xml version="1.0" encoding="utf-8"?> <conversation> <message sender="John">Hi, How are you.</message> </conversation>
-
Retrieve an existing XML file
If you already have an existing XML file and you just want to update it, like adding an element, you can use the load method of DOMDocument to retrieve the file. Just like save(), this method takes an XML filename as the first parameter.
In addition, if you want the formatOutput property to have an effect when loading the file, you need to set the preserveWhiteSpace property of DOMDocument to false, and those two properties should be set before the load() method.
$dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->load("messages-xml.php");
-
Get the root element of XML
After retrieving the XML file, you should get the first or root element of the XML so that you can append a child element to it. To get the root element, use the documentElement property of the DOMDocument object.
$root = $dom->documentElement;
-
Example of Updating an XML
<?php $dom = new DOMDocument(); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; $dom->load("sample.xml"); $root = $dom->documentElement; $element = $dom->createElement('message', 'Hi, I am fine.'); $element->setAttribute('sender', 'Michael'); $root->appendChild($element); $dom->save("sample.xml");
Output:
<?xml version="1.0" encoding="utf-8"?> <conversation> <message sender="John">Hi, How are your</message> <message sender="Michael">Hi, I am fine.</message> </conversation>
Summary
In this tutorial, I showed you how to manipulate an XML file in PHP. You have learned how to set the encoding and the version of the XML, give proper indentation and spaces to an XML, add an element and attribute to the XML, retrieve an existing XML file, and save an XML.
However, the objects, methods, and properties that we have discussed above are only a few of the things involved in manipulating XML. To learn more about this, you can read it on the XML Manipulation page of php.net.