note 85724 added to class.domelement

From: Date: Sun, 14 Sep 2008 13:18:21 +0000
Subject: note 85724 added to class.domelement
Groups: php.notes 
Request: Send a blank email to php-notes+get-144408@lists.php.net to get a copy of this message
I wanted to find similar Elements - thats why I built an Xpath-String like this - maybe somebody needs it... its not very pretty - but neither ist domdocument :) $dom->load($xmlFile)) $xpathQuery = '//*'; $xmlNodes = $xpath->query($xpathQuery); $pathlist = array(); $attrlist = array(); foreach ($xmlNodes as $node) { $depth = $this->_getDomDepth($node); //get Path-Depth (for array key) $pathlist[$depth] = $node->tagName; // tagname $attrs = $node->attributes; $attr=''; $a=0; foreach ($attrs as $attrName => $attrNode) // attributes { if ($attrName !='reg') { if ($a++!=0) $attr .= ' and '; $attr .= '@'.$attrName.'='."'".$attrNode->value."'"; } } $attrlist[$depth] = $attr?'['.$attr.']':''; $path = ''; for ($i=0;$i<=$depth;$i++) $path .= '/'.$pathlist[$i].$attrlist[$i]; // the xpath of the actual Element // ... now you can go on and user $path to find similar elements } } } private function _getDomDepth(DomNode $node) { $r = -2; while ($node) { $r++; $node = $node->parentNode; } return $r; } ---- Server IP: 81.223.48.75 Probable Submitter: 62.178.130.46 ---- Manual Page -- http://www.php.net/manual/en/class.domelement.php Edit -- https://master.php.net/note/edit/85724 Del: integrated -- https://master.php.net/note/delete/85724/integrated Del: useless -- https://master.php.net/note/delete/85724/useless Del: bad code -- https://master.php.net/note/delete/85724/bad+code Del: spam -- https://master.php.net/note/delete/85724/spam Del: non-english -- https://master.php.net/note/delete/85724/non-english Del: in docs -- https://master.php.net/note/delete/85724/in+docs Del: other reasons-- https://master.php.net/note/delete/85724 Reject -- https://master.php.net/note/reject/85724 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#144408) next »