rabby: HTML/XML Tree darstellen

Beitrag lesen

Oder besser noch: Diesen Schnipsel gabs im Internet zu finden und der funktioniert bestens:

function walkDom($node, $level = 0)  
{  
$indent = '';  
for ($i = 0; $i < $level; $i++)  
$indent .= '&nbsp;&nbsp;'; //prettifying the output  
if($node->nodeType != XML_TEXT_NODE)  
{  
echo $indent.'<b>'.$node->nodeName.'</b>';  
if( $node->nodeType == XML_ELEMENT_NODE )  
{  
$attributes = $node->attributes; // get all the attributes(eg: id, class …)  
foreach($attributes as $attribute)  
{  
echo ', '.$attribute->name.'='.$attribute->value;  
// $attribute->name is usually one of these:  
// src, type, rel, link, name, value, href, onclick,  
// id, class, style, title  
// You can add your custom handlers depending on the Attribute.  
}  
//if( strlen(trim($node->childNodes->item(0)->nodeValue)) > 0 && count($cNodes) == 1 )  
//echo '<br>'.$indent.'(contains='.$node->childNodes->item(0)->nodeValue.')'; // do this to print the contents of a node, which maybe the link text, contents of div and so on.  
}  
echo '<br><br>';  
}  
$cNodes = $node->childNodes;  
if (count($cNodes) > 0)  
{  
$level++ ; // go one level deeper  
foreach($cNodes as $cNode)  
walkDom($cNode, $level); //so this is recursion my professor kept talkin' about  
$level = $level - 1; // come a level up, and had to do it this way or else wordpress would take away one dash. :(  
}  
}