note 36588 added to function.domdocument-get-elements-by-tagname
| From: | nukebaghdad at rn2 dot php dot net | Date: | Wed, 15 Oct 2003 16:39:33 +0000 |
| Subject: | note 36588 added to function.domdocument-get-elements-by-tagname | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-58639@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.domdocument-get-elements-by-tagname.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+36588
Delete -- http://master.php.net/manage/user-notes.php?action=delete+36588&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+36588&report=yes
Search -- http://master.php.net/manage/user-notes.php