Thursday, August 28, 2008

Read XML Using PHP DOM

This tutorial is for PHP starters as well as for those who are using XML parser functions to read/write XML files. This tutorial will not go into details on XML. The focus will be more on the Document Object Model (DOM) extension.

This should not be confused with DOM XML extension, which is not packaged into PHP5 and will never be (according to DOM extension allows PHP users to use the DOM functions. The function then allow users to read, write, rename tags and do all types of crazy stuff with XML documents.

First we need a XML document to experiment with. I have created a simple XML document and named it readfromxml.xml


<1?xml version="1.0" encoding="ISO-8859-1"?>



<1pagename>Current page name<1/pagename>

<1title>Title for Pagename<1/title>

<1keyword>meta keyword for current page<1/keyword>

<1description>meta Description for current page<1/description>



This XML document stores pagename,title of the page,keyword for the page and description for the page. You can create one for any type of webpage to create keywords, description dynamically & manage for every single page. Every note in this XML file beings < with and ends with >. Within the tab I have created a tab for the keyword ( ),description ( ),title ( ),pagename ().

Next step is to read this XML and display the results on a web page.

Create another file and call it readfromxml.php. Copy the following code into it and run it.

// DOMElement->getElementsByTagName() -- Gets elements by tagname
// nodeValue : The value of this node, depending on its type.
// Load XML File. You can use loadXML if you wish to load XML data from a string

$objDOM = new DOMDocument();
$objDOM->load("readfromxml.xml"); //make sure path is correct

$note = $objDOM->getElementsByTagName("head");
// for each note tag, parse the document and get values for
// tasks and details tag.

foreach( $note as $value )

$page = $value->getElementsByTagName("pagename");
$page = $page->item(0)->nodeValue;
if($page == $pagename)
$title = $value->getElementsByTagName("title");
$title = $title->item(0)->nodeValue;

$keywords = $value->getElementsByTagName("keyword");
$keywords = $keywords->item(0)->nodeValue;

$description = $value->getElementsByTagName("description");
$description = $description->item(0)->nodeValue;


create a webpage as index.php and copy the following content and paste it in index.php

$pagename = "index.php";
<1!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<1html xmlns="">
<1meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />



This tutorial is a kick start for beginners. You can retreive data from a database and create a XML document on-the-fly with DOM functions, rename tabs in an existing XML document and validate format.

Note : Remove 1 on every tag

oDesk Certified Software Testing Engineer


Anonymous said...


your example is very good.

i have one xml file and i want to display xml data in web page as a table format using php and ajax and jquary

do you have any code for this.

Norbou said...

Thankx, but it doesn't work on my server. No matter.

I would like to ask you if is possible in the PHP document read the DOM and include on another place in the document the content of specified tag from current page written as HTML text or another static content. This can be used for repeat news headlines etc.

This will help me very. I don't use databases, only PHP.

Anonymous said...

I don't understand why you don't use SimpleXML to parse XML ??

phponwebsites said...

Thank you very much

Manjot kaur said...

Keep on posting these types of articles. I like your blog design as well. Cheers!!!

6 Month php Industrial Training in Chandigarh
6 weeks php industrial training in chandigarh

Naviya Nair said...

Great Article..
PHP Training in Chennai
Online PHP Training
Online PHP Training India
PHP Training Chennai
PHP Training institute in Chennai

Bhavya Kumar said...

My friend Suggest me this blog and I can say this is the best blog to get the basic knowledge.Thank you so much for this Selenium Training in Chennai

CIIT Noida said...

CIIT Noida provides Best MCA Courses in Noida based on the current IT industry standards that help students to get high paying jobs in Top MNCs. CIIT provides Best MCA Training in Noida, Greater Noida, and Ghaziabad. CIIT is one of the trusted MCA training institutes in Noida providing practical knowledge and 100% job assistance with basic as well as advanced level MCA subjects. CIITN is the best MCA college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin.

At CIIT MCA classes in Noida is conducted by subject experts corporate professionals with 9+ years of experience in managing real-time and live projects. Sofracle Nano Specialized MCA classes Noida is the perfect blend of academic learning and practical sessions to provide maximum exposure to students that transform an average student into a corporate professional whom companies prefer to hire.

Best MCA College in Noida