|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2002-12-12 01:21 UTC] chregu@php.net
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 13:00:01 2025 UTC |
The function DomElement->get_elements_by_tagname() is now recursive. It is returning matching elements from it's children nodes as well. In 4.2.3, this function would only return matching elements in it's node. The following code (with output) shows the difference. #!/usr/bin/php -q <?php $xmlsource = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"; $xmlsource .= "<root comment=\"root node\">"; $xmlsource .= " <one comment=\"one\">"; $xmlsource .= " <two comment=\"first two, count should be 1\">"; $xmlsource .= " <three comment=\"three\">"; $xmlsource .= " </three>"; $xmlsource .= " </two>"; $xmlsource .= " <four comment=\"four\">"; $xmlsource .= " <two comment=\"second two\">"; $xmlsource .= " </two>"; $xmlsource .= " </four>"; $xmlsource .= " </one>"; $xmlsource .= "</root>"; $doc = domxml_open_mem($xmlsource); echo "==========================================================\r\n"; echo "PHP Version: ".phpversion()."\r\n"; echo "==========================================================\r\n"; echo sprintf("\$doc->get_elements_by_tagname(\"two\"): %d\r\n", count($doc->get_elements_by_tagname("two"))); echo "----------------------------------------------------------\r\n"; echo " Count | Node | Comment\r\n"; echo "----------------------------------------------------------\r\n"; // Set root node $node_root = $doc->document_element(); // Find node_one $array_nodes = $node_root->get_elements_by_tagname("one"); if (count($array_nodes) > 0) { $node_one = $array_nodes[0]; // echo sprintf("[%d] node_one comment: %s\r\n", count($array_nodes), $node_one->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_one", $node_one->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_one", "Not Found"); } // Find node_firstTwo $array_nodes = $node_one->get_elements_by_tagname("two"); if (count($array_nodes) > 0) { $node_firstTwo = $array_nodes[0]; // echo sprintf("[%d] node_firstTwo comment: %s\r\n", count($array_nodes), $node_firstTwo->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_firstTwo", $node_firstTwo->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_firstTwo", "Not Found"); } // Find node_four $array_nodes = $node_root->get_elements_by_tagname("four"); if (count($array_nodes) > 0) { $node_four = $array_nodes[0]; // echo sprintf("[%d] node_four comment: %s\r\n", count($array_nodes), $node_four->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_four", $node_four->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_four", "Not Found"); } // Find node_secondTwo if ($node_four) { $array_nodes = $node_four->get_elements_by_tagname("two"); if (count($array_nodes) > 0) { $node_secondTwo = $array_nodes[0]; // echo sprintf("[%d] node_secondTwo comment: %s\r\n", count($array_nodes), $node_secondTwo->get_attribute("comment")); echo sprintf(" %-5d | %-15s | %s\r\n", count($array_nodes), "node_secondTwo", $node_secondTwo->get_attribute("comment")); } else { echo sprintf(" %-5d | %-15s | %s\r\n", 0, "node_secondTwo", "Not Found"); } } echo "==========================================================\r\n"; ?> Output for PHP 4.2.3: ========================================================== PHP Version: 4.2.3 ========================================================== $doc->get_elements_by_tagname("two"): 2 ---------------------------------------------------------- Count | Node | Comment ---------------------------------------------------------- 1 | node_one | one 1 | node_firstTwo | first two, count should be 1 0 | node_four | Not Found ========================================================== Output for PHP 4.3.0RC3: ========================================================== PHP Version: 4.3.0RC3 ========================================================== $doc->get_elements_by_tagname("two"): 2 ---------------------------------------------------------- Count | Node | Comment ---------------------------------------------------------- 1 | node_one | one 2 | node_firstTwo | first two, count should be 1 1 | node_four | four 1 | node_secondTwo | second two ========================================================== Hopefully you can see what I'm trying to show here. The fact that 4.3.0RC3 finds the node_four and thus the node_secondTwo shows that DomElement->get_elements_by_tagname is recursive. Since PHP support for DOM XML is experimental, this might be a feature rather than a bug. If that's the case, maybe a parameter could be added to the function to select whether or not to traverse deeper into the tree. Thanks Dave Note: this was tested with libxml2-2.4.21 installed. Everything on the system is identical for each run above except for the php binary.