note 36588 deleted from function.domdocument-get-elements-by-tagname by bjori

From: Date: Wed, 03 Jan 2007 07:59:41 +0000
Subject: note 36588 deleted from function.domdocument-get-elements-by-tagname by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-121226@lists.php.net to get a copy of this message
Note Submitter: nukebaghdad at yahoo dot co dot uk ---- Umm.... PHP4.2.3; Having some bother using DOMs get_elements_by_tagname('tag') myself on an XML and xHTML document for an unknown reason, I've now scripted a recursive function to traverse a document looking for a given tag; $dom = domxml_open_file('c:/program files/apache group/apache/htdocs/template.html'); $doc = $dom -> document_element(); // echo('<pre>'); print_r($doc -> child_nodes()); echo('</pre>'); $doc = $doc -> child_nodes(); # $head = $doc[1]; $body = $doc[3]; # $node = findTag($body, 'phptag'); echo('<pre>'); print_r($node); echo('</pre>'); # function findTag($node, $tag) { if($node -> has_child_nodes()) { # continue as there are children to this node $children = $node -> child_nodes(); # foreach($children as $child) { # first, check to see if a child is an actual node, and not just #text if($child -> type == XML_ELEMENT_NODE) { # we have a real node, now check against the one were looking for if($child -> tagname() == $tag) { # found it return $child; } else { $a = findTag($child, $tag); } } } return $a; } } Not too sure why I just couldn't use get_elements_by_tagname() though this function does solve this problems I've been having, hope it helps others. Enjoy.

« previous php.notes (#121226) next »