rabby: HTML/XML Tree darstellen

Beitrag lesen

function PrintDomTree($DomNode)
{
    if ($ChildDomNode = $DomNode->first_child()) {
        static $depth = 0;

$whitespace = "\n<br>".str_repeat(" ", ($depth * 2));

while ($ChildDomNode) {
            if ($ChildDomNode->node_type() == XML_TEXT_NODE) {
                echo trim($ChildDomNode->node_value());
            } elseif ($ChildDomNode->node_type() == XML_ELEMENT_NODE) {
                $HasTag = 1;
                echo $whitespace;
                echo "<", $ChildDomNode->node_name();

if ($ChildDomNode->has_attributes()) {
                    $Array = $ChildDomNode->attributes();
                    foreach ($Array AS $DomAttribute) {
                        echo " ", $DomAttribute->name(), "="", $DomAttribute->value(), """;
                    }
                }
                echo ">";

if ($ChildDomNode->has_child_nodes()) {
                    $depth++;
                    if (PrintDomTree($ChildDomNode)) {
                        echo $whitespace;
                    }
                    $depth--;
                }
                echo "</", $ChildDomNode->node_name(), ">";
            }
            $ChildDomNode = $ChildDomNode->next_sibling();
        }
        return $HasTag;
    }
}