Generate xml file with PHP DOMDocument class
DOMDocument class is part of the PHP DOM extension (Document Object Model) and allows us to interact with XML as well as HTML documents.
Today we will learn how to generate an xml document out of an array.
To start with create a new file in the root of your project called index.php and open it for editing.
Right at the top start with the opening php tag, header informing the browser of the type of document and the definition of our array:
<?php
header('Content-Type: text/xml');
$array = array(
array(
'loc' => 'http://www.ssdtutorials.com',
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 1
),
array(
'loc' => 'http://www.coremediadesign.co.uk',
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.8
),
array(
'loc' => 'http://www.google.co.uk',
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.5
)
);
Now we are going to instantiate the DOMDocument class and set values for a few properties of this class:
$objDom = new DOMDocument('1.0');
$objDom->encoding = 'UTF-8';
$objDom->formatOutput = true;
$objDom->preserveWhiteSpace = false;
The parameter passed to the DOMDocument object is the version of the XML document we want to use. We have set the character encoding to be UTF-8, output to be nicely formatted with indentation and extra space, and indicated that we do not want to preserve white space.
We are now ready to create the first node of our new xml document called urlset with its xmlns attribute:
$root = $objDom->createElement("urlset");
$objDom->appendChild($root);
$root_attr = $objDom->createAttribute("xmlns");
$root->appendChild($root_attr);
$root_attr_text = $objDom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
$root_attr->appendChild($root_attr_text);
Above we have first created the urlset element using createElement method - then appended it to the document using appendChild method.
We then created the attribute called xmlns and appended it to the urlset element. The attribute is then assigned a value first creating it using createTextNode, followed by assignment with appendChild method.
Next we are going check if our array is not empty and if not - we will loop through its values:
if (!empty($array)) {
foreach($array as $row) {
}
}
Inside of the foreach loop start with creating the new element called url and append it to the $root, which will put it in between the opening and closing urlset tags:
$url = $objDom->createElement("url");
$root->appendChild($url);
Now let's create all four nodes : loc, lastmod, changefreq and priority:
$loc = $objDom->createElement("loc");
$lastmod = $objDom->createElement("lastmod");
$changefreq = $objDom->createElement("changefreq");
$priority = $objDom->createElement("priority");
Next, append them to the url element and assign the value to each, using createTextNode and appendChild methods:
$url->appendChild($loc); $url_text = $objDom->createTextNode($row["loc"]); $loc->appendChild($url_text); $url->appendChild($lastmod); $lastmod_text = $objDom->createTextNode($row["lastmod"]); $lastmod->appendChild($lastmod_text); $url->appendChild($changefreq); $changefreq_text = $objDom->createTextNode($row["changefreq"]); $changefreq->appendChild($changefreq_text); $url->appendChild($priority); $priority_text = $objDom->createTextNode($row["priority"]); $priority->appendChild($priority_text);
Now our document is ready.
The last thing we need to do is to output it - and this is simply done by using saveXML method:
echo $objDom->saveXML();
Your entire file should now look similar to this:
<?php
header('Content-Type: text/xml');
$array = array(
array(
'loc' => 'http://www.ssdtutorials.com',
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 1
),
array(
'loc' => 'http://www.coremediadesign.co.uk',
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.8
),
array(
'loc' => 'http://www.google.co.uk',
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.5
)
);
$objDom = new DOMDocument('1.0');
$objDom->encoding = 'UTF-8';
$objDom->formatOutput = true;
$objDom->preserveWhiteSpace = false;
$root = $objDom->createElement("urlset");
$objDom->appendChild($root);
$root_attr = $objDom->createAttribute("xmlns");
$root->appendChild($root_attr);
$root_attr_text = $objDom->createTextNode("http://www.sitemaps.org/schemas/sitemap/0.9");
$root_attr->appendChild($root_attr_text);
if (!empty($array)) {
foreach($array as $row) {
$url = $objDom->createElement("url");
$root->appendChild($url);
$loc = $objDom->createElement("loc");
$lastmod = $objDom->createElement("lastmod");
$changefreq = $objDom->createElement("changefreq");
$priority = $objDom->createElement("priority");
$url->appendChild($loc);
$url_text = $objDom->createTextNode($row["loc"]);
$loc->appendChild($url_text);
$url->appendChild($lastmod);
$lastmod_text = $objDom->createTextNode($row["lastmod"]);
$lastmod->appendChild($lastmod_text);
$url->appendChild($changefreq);
$changefreq_text = $objDom->createTextNode($row["changefreq"]);
$changefreq->appendChild($changefreq_text);
$url->appendChild($priority);
$priority_text = $objDom->createTextNode($row["priority"]);
$priority->appendChild($priority_text);
}
}
echo $objDom->saveXML();
If you now call your index.php in the browser you should see the following:



