Read and Write an XML File using PHP

How to read and write xml document with php’s DOMDocument. The DOM extension allows you to operate on XML documents through the DOM API with PHP 5. For better understanding, I have written comment for almost every line of code.

XML Code: file name – employee.xml

[xml]
<?xml version="1.0"?>
<employees>
<employee id="1" name="Albert"><age>34</age><![CDATA[
Employee details
]]><?salary $10000?></employee>
<employee id="2" name="Claud"><age>20</age><![CDATA[
Employee details
]]><?salary $2000?></employee>
</employees>
[/xml]

Reading an XML File using PHP

PHP Code:

[php]
<?php
// Create a new DOMDocument object
$doc = new DOMDocument();

// Load XML from a file
$doc->load( ’employee.xml’ );
//echo $doc->childNodes->length;

// Search for all elements with tag name ‘brand’
$employees = $doc->getElementsByTagName(’employee’);

//echo $employees->item(0)->getAttribute(‘id’);

// loop through brands and print brand name and price
foreach ($employees as $key => $employee) {

// print attribute value
echo $employee->getAttribute(‘id’). "<br />";
echo $employee->getAttribute(‘name’). "<br />";

// search for element ‘age’
$age = $doc->getElementsByTagName(‘age’);
// print node value
echo $age->item($key)->nodeValue. "<br />";

}
?>
[/php]

With the above XML file the PHP code will read the XML file and retrieve the information from it and output that in the screen.

Writing XML using PHP

PHP Code:

[php]
<?php
// Initial Array values
$employees = array();
$employees [] = array(
‘id’ => 1,
‘name’ => ‘Albert’,
‘age’ => ’34’,
‘salary’ => "$10000"
);
$employees [] = array(
‘id’ => 2,
‘name’ => ‘Claud’,
‘age’ => ’20’,
‘salary’ => "$2000"
);

// create doctype
$doc = new DOMDocument();
$doc->formatOutput = true;

// create root element
$r = $doc->createElement( "employees" );
$doc->appendChild( $r );

foreach( $employees as $employee )
{
// create child element
$b = $doc->createElement( "employee" );

// create attribute node
$id = $doc->createAttribute("id");
$b->appendChild($id);

// create attribute value node
$idValue = $doc->createTextNode( $employee[‘id’] );
$id->appendChild($idValue);

// add new attribute
$b->setAttribute(‘name’, $employee[‘name’] );

// create sub-child element
$age = $doc->createElement( "age" );
$b->appendChild( $age );

// create text node
$age->appendChild( $doc->createTextNode( $employee[‘age’] ) );

// create CDATA section
$cdata = $doc->createCDATASection("nEmployee detailsn");
$b->appendChild($cdata);

// create PI
$pi = $doc->createProcessingInstruction("salary", $employee[‘salary’]);
$b->appendChild($pi);

$r->appendChild( $b );

}

// save tree to string
echo $employeeDetails = $doc->saveXML();

// save tree to file
$doc->save("employee.xml")
?>
[/php]

This code will output the XML file in console as well as it will create a XML file in the name of employee.xml in the same directory of the PHP script.

Permanent link to this article: https://blog.openshell.in/2011/07/read-and-write-an-xml-file-using-php/

Leave a Reply

Your email address will not be published.