<html>
<head>
</head>
<body bgcolor="white">
<hr>
<?
// data file
$file = "may.xml";
// hash for HTML markup
$startTags = array(
"hosts" => "<font size=+2>",
"host" => "<i>(",
"name" => "<b>",
"ip" => "<i>Serves ",
"os" => "<h3>Ingredients:</h3><ul>",
"hostname" => "<li>",
"resolved" => "(",
);
$endTags = array(
"hosts" => "</font><br>",
"host" => ")</i>",
"name" => "</b>",
"ip" => "</i>",
"os" => "</ul>",
"hostname" => ")",
"resolved" => "</ol>",
);
// create a document object
$dom = xmldocfile($file);
// get reference to root node
$root = $dom->root();
// get children
$children = $root->children();
// run a recursive function starting here
printData($children);
// this function accepts an array of nodes as argument,
// iterates through it and prints HTML markup for each tag it finds.
// for each node in the array, it then gets an array of the node's
//children, and
// calls itself again with the array as argument (recursion)
function printData($nodeCollection)
{
global $startTags, $endTags;
// iterate through array
for ($x=0; $x<sizeof($nodeCollection); $x++)
{
// print HTML opening tags and node content
echo $startTags[$nodeCollection[$x]->name] . $nodeCollection[$x]->content;
// get children and repeat
$dummy = getChildren($nodeCollection[$x]);
printData($dummy);
// once done, print closing tags
echo $endTags[$nodeCollection[$x]->name];
}
}
// function to return an array of children, given a parent node
function getChildren($node)
{
$temp = $node->children();
$collection = array();
$count=0;
// iterate through children array
for ($x=0; $x<sizeof($temp); $x++)
{
// filter out the empty nodex
// and create a new array
if ($temp[$x]->type == XML_ELEMENT_NODE)
{
$collection[$count] = $temp[$x];
$count++;
}
}
// return array containing child nodes
return $collection;
}
?>
</body>
</html>