PHP | DOMDocument getElementsByTagName() Function

The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name.

Syntax:

DOMNodeList DOMDocument::getElementsByTagName( string $name )

Parameters: This function accepts single parameter $name which holds the local tag name to match. The value * is used to match all tags.



Return Value: This function returns a new DOMNodeList object containing all the matched elements.

Below program illustrates the DOMDocument::getElementsByTagName() function in PHP:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Store the XML document to the variable
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<organization>
    <name>GeeksforGeeks</name>
    <address>Noida India</address>
    <contact>
        <email>abc@geeksforgeeks.org</email>
        <mobile>+91-987654321</mobile>
    </contact>
</organization>
XML;
  
// Create new DOMDocument
$dom = new DOMDocument;
  
// Load the XML document
$dom->loadXML($xml);
  
// Use getElementsByTagName() function to search
// all elements with given local tag name
$org = $dom->getElementsByTagName('contact');
  
foreach ($org as $contact) {
    echo $contact->nodeValue, PHP_EOL;
}
?>

chevron_right


Output:

abc@geeksforgeeks.org
+91-987654321

Reference: https://www.php.net/manual/en/domdocument.getelementsbytagname.php



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.