Re: Prevent XSS using DOM Extension and/or SimpleXML

From: Date: Tue, 14 Nov 2006 12:28:12 +0000
Subject: Re: Prevent XSS using DOM Extension and/or SimpleXML
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-244594@lists.php.net to get a copy of this message
Raphael Martins wrote:
But I don´t know how to loop over every attribute for each tag in the DomTree.
Not sure if you need to do this element by element or just want all attributes, but here are two ways using DOM. They assume $dom is an already loaded DOMDocument. 1 - Use XPath: $xPath = new DOMXPath($dom); $nodelist = $xPath->query("//@*"); foreach ($nodelist AS $attr) { print $attr->nodeName." ".$attr->nodeValue."\n"; } 2 - Walk the tree manually: function checkElement($node) { $attlist = $node->attributes; foreach ($attlist AS $attr) { print $attr->nodeName." ".$attr->nodeValue."\n"; } if ($node->hasChildNodes()) { foreach ($node->childNodes AS $child) { if ($child->nodeType == XML_ELEMENT_NODE) { checkElement($child); } } } } $root = $dom->documentElement; checkElement($root); Rob

« previous php.general (#244594) next »