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=" echo $description;?>" />
<1META NAME="KEYWORDS" CONTENT=" echo $keywords;?>" />
<1title> echo $title;?><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


2 comments:
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.
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.
Post a Comment