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 php.net). 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


readfromxml.xml


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

<1html>

<1head>

<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>

<1/head>

<1/html>


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";
require("readfromxml.php");
?>
<1!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<1html xmlns="http://www.w3.org/1999/xhtml">
<1head>
<1meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<1META NAME="DESCRIPTION" CONTENT="" />
<1META NAME="KEYWORDS" CONTENT="" />

<1title><1/title>
<1/head>

<1body>
<1/body>
<1/html>



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

6 comments:

Anonymous said...

hi

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

navya said...

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

ciitnoida 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