PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument::getElementsByTagNameNS> <DOMDocument::getElementById
Last updated: Fri, 28 Nov 2008

view this page in

DOMDocument::getElementsByTagName

(No version information available, might be only in CVS)

DOMDocument::getElementsByTagNameSearches for all elements with given tag name

Description

DOMNodeList DOMDocument::getElementsByTagName ( string $name )

This function returns a new instance of class DOMNodeList containing the elements with a given tag name.

Parameters

name

The name of the tag to match on. The special value * matches all tags.

Return Values

A new DOMNodeList object containing all the matched elements.



add a note add a note User Contributed Notes
DOMDocument::getElementsByTagName
StudioAMK.com
09-Oct-2008 08:09
<?php

$doc
= new DOMDocument();
$doc->load( 'Users.xml' );
 
$dataset = $doc->getElementsByTagName( "dataUser" );
foreach(
$dataset as $row )
{
   
$xmlUserNames = $row->getElementsByTagName( "UserName" );
   
$xmlUserName = $xmlUserNames->item(0)->nodeValue;
 
   
$xmlEmails = $row->getElementsByTagName( "Email" );
   
$xmlEmail = $xmlEmails->item(0)->nodeValue;
 
   
$xmlDisplayNames = $row->getElementsByTagName( "DisplayName" );
   
$xmlDisplayName = $xmlDisplayNames->item(0)->nodeValue;
 
    echo
"$xmlUserName - $xmlEmail - $xmlDisplayName\n";
}
?>

Contents in Users.xml

<NewDataSet>
    <dataUser>
        <UserName>StudioAMK</UserName>
        <Email>user1@mail.com</Email>
        <DisplayName>StudioAMK.com</DisplayName>
    </dataUser>
    <dataUser>
        <UserName>User2</UserName>
        <Email>user2@mail.com</Email>
        <DisplayName>UserTwo</DisplayName>
    </dataUser>
</NewDataSet>
James L
19-Aug-2008 04:04
Return if there are no matches is an empty DOMNodeList. Check using length property, e.g.:

<?php
$nodes
=$domDocument->getElementsByTagName('book') ;
if (
$nodes->length==0) {
  
// no results
}
?>
jason at shaped dot ca
11-Feb-2008 04:59
in response to tildy at pr dot hu

my preferred way is (in example to gather country data from an iso 3166 xml flie)

$countries = new DOMDocument();
$countries->load("./lib/iso_3166.xml");

$countriesList = $countries->getElementsByTagName("ISO_3166-1_Entry");

foreach($countriesList as $country) {
    $values = $country->getElementsByTagName("*");
    foreach($values as $node) {
      echo $node->nodeName."=".$node->nodeValue;
    }
}
tildy at pr dot hu
13-Dec-2007 04:51
If you want to list the nodename and value of one item(node)  this is an example:

$itemnodes = $doc->getElementsByTagName( "item" );
$nodes = $itemnodes->item(0)->getElementsByTagName( "*" );
for ( $i = 0; $i < $nodes->length; $i++ ) {
    print "nodename=".$nodes->item( $i )->nodeName;
    print "\t";
    print "nodevalue : ".$nodes->item( $i )->nodeValue;
    print "\r\n";
}

It will be list all children name and value of item.
Francois Hill
30-Jul-2007 07:36
Careful : getElementsByTagName will yield all elements with the given tag name under the present node, at any sub-level (i.e. among child nodes and all other descendant nodes)
Finding values of a node
14-Mar-2007 03:01
I don't know if this is that obvious but it was not for me, so in addition to gem at rellim dot com's posting:
adding

<?php
echo $param -> nodeValue.'<br>';
?>

to the loop will output
value1
value2
value3
gem at rellim dot com
29-Sep-2004 04:20
Here is an example of getElementsByTagName():

<?php
$xml
=<<<EOT
<?xml version="1.0"?>
<config>
  <section id="section1">
   <param name="param1">value1</param>
   <param name="param2">value2</param>
  </section>
  <section id="section2">
   <param name="param3">value3</param>
  </section>
</config>
EOT;

$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($xml);
$params = $dom->getElementsByTagName('param');

foreach (
$params as $param) {
       echo
$param -> getAttribute('name').'<br>';
}
?>

Expected result:
--------------
param1
param2
param3

 
show source | credits | sitemap | contact | advertising | mirror sites