DomDocument->get_elements_by_tagname

(no version information, might be only in CVS)

DomDocument->get_elements_by_tagname --  Returns array with nodes with given tagname in document or empty array, if not found

说明

array DomDocument->get_elements_by_tagname ( string name )

See also domdocument_add_root()


add a note add a note User Contributed Notes
nick dot dixon at NOSPAM dot mi-int dot com
04-Mar-2004 11:04
For nukebagdad: in PHP versions below 4.3.0, get_elements_by_tagname() is not recursive; that is, it will only look at immediate children inside the element, and will not look any deeper in the tree.

So if you have a document:
<root>
  <a>
   <b>
     <c>
     </c>
   </b>
  </a>
</root>

calling:
   $a->get_elements_by_tagname("b")

returns an array containing the element <b>, as expected.

But if you try:
   $a->get_elements_by_tagname("c")

you will get an empty array (I'm seeing this in 4.2.2).

The method was made recursive in 4.3.0RC4 (see http://bugs.php.net/bug.php?id=20948)
jon at hiveminds dot net
05-Dec-2003 09:49
Warning to win32 users: It appears that use of the wildcard argument to this function (e.g.

<?php
  $alltags
= $domdoc->get_elements_by_tagname("*");
?>

) to return a collection of all DomElements in a document, which worked in php4.3.0-4.3.3, is broken in php4.3.4 for Windows. Upgrading broke numerous scripts depending on this behaviour, which returned to normal as soon as I rolled back to 4.3.3-win32.

I had intended to offer a workaround using child_nodes(), but found problems with this also. Therefore my suggested workarounds are either (a) don't upgrade to 4.3.4 if you're running this extension on Windows and are depending on the wildcard behaviour or (b) rewrite your code so as to use actual tagnames

The wildcard usage is per the W3C DOM spec, so hopefully this will be fixed and available again in 4.3.5.
nukebaghdad at yahoo dot co dot uk
16-Oct-2003 03:39
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.
Salman
02-Oct-2003 07:23
Yes it does return an object; the object is of type DomElement. Hopefully this code will help:

$element = new DomElement();

if(!$dom = domxml_open_file($xml_file)) {
   die("Error opening xml file");
}

$tables = $dom->get_elements_by_tagname("table");
foreach($tables as $table) {
   $element = $table;
   echo "Attribute name: ", $element->get_attribute("name");
}

- Salman
http://wwww.setcomputing.com/
blah at pasher dot org
17-Jan-2003 03:56
Correction to my last note: DomElement (and DomDocument) have their own get_elements_by_tagname() method. It does not get it from DomNode.
blah at pasher dot org
17-Jan-2003 03:44
To clarify some confusion:

This function returns an integer-indexed array of DomElement objects. The DomElement class extends the DomNode class, so all DomNode methods are available to this new object. Additional notes can be found under the "DomElement->get_elements_by_tagname" manual section. DomElement uses the DomNode method (unless DomElement overrides this method, but I don't think that occurs).

Also note, if there are no nodes matching your criteria, it returns an empty array, not false.
KingGeoffrey at yahoo dot com
24-Aug-2002 06:21
The return looks like an integer indexed array of objects when i try it.

Versions
 php:4.2.1 domxml:2.4.9

Code fragment:
   $nodes = $doc->get_elements_by_tagname("user");
   reset($nodes);
   while (list($key, $value) = each($nodes)) {
       echo "$key = " . $value->get_attribute('username') . "<br />\n";

   }

Output fragment:
0 = a
1 = b
2 = c
scouture at courtweb dot com
16-Jul-2002 06:33
Seems that this function return an object instead of an array. I've tried this :

if(!$dom = domxml_open_file("g://program files/apache group/apache/htdocs/intranetcw/data/config_one.xml"))
{
  echo "Error while parsing the document\n";
  exit;
}

//array DomDocument->get_elements_by_tagname ( string name)

$array_element = $dom->get_elements_by_tagname("IP_ORACLE_CONFIG");
if ($array_element)
{
   echo count($array_element)." array count<br>";
   for ($i=0;$i<count($array_element);$i++)
   {
       echo $array_element[$i]." array content<br>";
   }
}
else
{
   echo "you get nothing";
}

and the result was :

1 array count
Object array content

Styve