First, let's look at what the final output should look like. The generated version will have slightly different formatting and indentation, but the functionality will be the same.
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DOMDocument</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="wrapper">
<div id="header"><img src="header.gif" alt="Header" width="400" height="100" /></div>
<div id="nav">
<ul>
<li class="active"><a href="index.php">Home</a></li>
<li><a href="download.php">Download</a></li>
<li><a href="features.php">Features</a></li>
<li><a href="about.php">About</a></li>
</ul>
</div>
<div id="content">
<h1>DOMDocument</h1>
<p>This page was generated using PHPs <strong>DOMDocument</strong> class!</p>
</div>
</div>
</body>
</html>
So now let's start writing code! Below each snippet is an explanation.
Code: Select all
<?php
// Create document
$doctype = DOMImplementation::createDocumentType('html',
'-//W3C//DTD XHTML 1.1//EN',
'http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd');
$document = DOMImplementation::createDocument('http://www.w3.org/1999/xhtml',
'html',
$doctype);
Here is where we setup the document, using a special class called DOMImplementation that allows us to easily create a document with the appropriate DOCTYPE declaration. The createDocumentType method stores the document type information in an object to be passes to the createDocument method, which actually creates the document. This document has the root <html> tag already created.
If you compare the call to createDocumentType to the <!DOCTYPE line of the XHTML, you can see where the 3 parameters come from. The createDocument class accepts the value of the xmlns attribute of the html tag, the name of the root element (<html>), and the object created by createDocumentType as parameters.
Code: Select all
// Create head element
$head = $document->createElement('head');
Code: Select all
$metahttp = $document->createElement('meta');
$metahttp->setAttribute('http-equiv', 'Content-Type');
$metahttp->setAttribute('content', 'text/html; charset=utf-8');
$head->appendChild($metahttp);
$title = $document->createElement('title', 'DOMDocument');
$head->appendChild($title);
$css = $document->createElement('link');
$css->setAttribute('href', 'styles.css');
$css->setAttribute('rel', 'stylesheet');
$css->setAttribute('type', 'text/css');
$head->appendChild($css);
This segment also demonstrates setting attributes with the DOMElement::setAttribute method, which simply accepts the attribute name and value as parameters.
Code: Select all
// Create body element
$body = $document->createElement('body');
// Wrapper div
$wrapper = $document->createElement('div');
$wrapper->setAttribute('id', 'wrapper');
Code: Select all
// Header div
$header = $document->createElement('div');
$header->setAttribute('id', 'header');
$wrapper->appendChild($header);
// Header img
$himg = $document->createElement('img');
$himg->setAttribute('src', 'header.gif');
$himg->setAttribute('alt', 'Header');
$himg->setAttribute('width', '400');
$himg->setAttribute('height', '100');
$header->appendChild($himg);
Code: Select all
// Nav div
$nav = $document->createElement('div');
$nav->setAttribute('id', 'nav');
$wrapper->appendChild($nav);
// Nav ul
$navlist = $document->createElement('ul');
$nav->appendChild($navlist);
Code: Select all
$menuArray = array();
$menuArray['index.php'] = 'Home';
$menuArray['download.php'] = 'Download';
$menuArray['generate.php'] = 'Generate';
$menuArray['about.php'] = 'About';
$currentPage = basename($_SERVER['SCRIPT_FILENAME']);
foreach($menuArray as $page => $title) {
$navitem = $document->createElement('li');
if($page == $currentPage) {
$navitem->setAttribute('class', 'active');
}
$navlink = $document->createElement('a', $title);
$navlink->setAttribute('href', $page);
$navitem->appendChild($navlink);
$navlist->appendChild($navitem);
}
Code: Select all
// Content div
$content = $document->createElement('div');
$content->setAttribute('id', 'content');
$wrapper->appendChild($content);
// Actual page content
$h1 = $document->createElement('h1', 'DOMDocument');
$content->appendChild($h1);
Code: Select all
$p = $document->createElement('p');
$text = $document->createTextNode('This page was generated using PHP\'s ');
$p->appendChild($text);
$text = $document->createElement('strong', 'DOMDocument');
$p->appendChild($text);
$text = $document->createTextNode(' class!');
$p->appendChild($text);
$content->appendChild($p);
Code: Select all
$body->appendChild($wrapper);
// Add head and body to document
$html = $document->getElementsByTagName('html')->item(0);
$html->appendChild($head);
$html->appendChild($body);
Code: Select all
// Output document
$document->formatOutput = true;
echo $document->saveXML();
Note: I used saveXML instead of saveHTML since XHTML documents follow some of the same standards as XML, such as self-closing tags using <tag />.