Home » PHP, XML

PHP : Read Write Xml with DOMDocument

23 October 2009 2,917 views Popularity: 5% Share/Bookmark

email

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 />";
}
?>

Related posts:

  1. PHP : Read Write Xml with SimpleXML
  2. Magento: Read Write XML
  3. PHP: Read Write CSV
  4. Magento: Read config XML nodes
  5. Javascript: Add Remove HTML elements
  6. PHP: Easily create PDF on the fly
  • http://pulse.yahoo.com/_7DIMVEFI2ESNZP4CLH6XIDSN3Q jarnail s

    Hi Mukesh,
    I want to print the XML output in browser.
    Thanks

  • Bacurastone

    help me,

    i used php DomDocument to read my xml file from external server, i’am using php vser 5.2.6
    my php script is like this
    $dom=new domDocument(“1.0″);
    $dom->load(“http://mysite.com/file/xml”);

    i can display my xml data with out any error.
    but, when xml data it’s updated (mean that i add some node on it).
    my script cannot display update item. like no chage have done in my xml file.
    i don’t know what this problem!
    is there is something wrong with my php.ini file configuration.
    help me please!

    regard saiful anwar.
    thank in advance!