In this article, I will be showing you how to create and read xml document with php’s DOMDocument.
The DOM extension allows you to operate on XML documents through the DOM API with PHP 5.
Below is the code to create XML file. I have named the output file ‘example.xml’. For better understanding, I have written comment for almost every line of code.
<?php
// Create a new DOMDocument object
$dom = new DomDocument("1.0", "ISO-8859-1");
// optionally the following can be done
// $dom->version = "1.0";
// $dom->encoding = "ISO-8859-1";
// Create new element node 'mobiles'
$mobiles = $dom->createElement('mobiles');
// add $mobiles to the main dom
$dom->appendChild($mobiles);
// Suppose we have the following array of data
$items = array(array('name'=>'samsung','price'=>'21000'),
array('name'=>'sony','price'=>'25000'));
foreach($items as $item) {
// Create new element node 'brand'
$brand = $dom->createElement('brand');
// Create new element node 'name' with the node value
$name = $dom->createElement('name',$item['name']);
// Create new element node 'price' with the node value
$price = $dom->createElement('price',$item['price']);
// add 'name' node to 'brand' node
$brand->appendChild($name);
// add 'price' node to 'brand' node
$brand->appendChild($price);
// add brand to mobile
$mobiles->appendChild($brand);
}
// add spaces, new lines and make the XML more readable format
$dom->formatOutput = true;
// SET HEADER TO OUTPUT DATA
header('HTTP/1.1 200 OK');
header("Pragma: no-cache");
header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');
header('Content-type', 'text/xml');
header('Content-Disposition: attachment; filename="example_dom.xml"');
// Dump the internal XML tree back into a string
echo $dom->saveXML();
?>
The following xml file is created. The xml file is named example_dom.xml.
<?xml version="1.0" encoding="ISO-8859-1"?>
<mobiles>
<brand>
<name>samsung</name>
<price>21000</price>
</brand>
<brand>
<name>sony</name>
<price>25000</price>
</brand>
</mobiles>
Here is the code to read the xml file. I have mentioned two ways to read the xml file. The first one loads the xml file, converts the data into string and prints it.
<?php
// Create a new DOMDocument object
$xmlDoc = new DOMDocument();
// Load XML from a file
$xmlDoc->load("example_dom.xml");
// Dump the internal XML tree back into a string
echo $xmlDoc->saveXML();
?>
The second way is to loop through the xml content and print. It’s more specific.
<?php
// Create a new DOMDocument object
$xmlDoc = new DOMDocument();
// Load XML from a file
$xmlDoc->load("example_dom.xml");
// Search for all elements with tag name 'brand'
$brands = $xmlDoc->getElementsByTagName('brand');
// loop through brands and print brand name and price
foreach ($brands AS $key => $brand) {
// search for element 'name'
$name = $xmlDoc->getElementsByTagName('name');
// search for element 'price'
$price = $xmlDoc->getElementsByTagName('price');
// print node value for name and price
echo $name->item($key)->nodeValue . " = " . $price->item($key)->nodeValue . "<br />";
}
?>
Hope it helps. Thanks.