Speed up development with full-stack environments for every branch.

Learn More

How to search for all elements with given local tag name with DOMDocument class [PHP]

40 Runs 140 Views 10 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * PHP DOM module
 * Search for all elements with given local tag name with DOMDocument class
 * 
 */

//setting div style for nicer display
$divStyle = ' background-color:#E8E8E3;
            padding:10px;
            color:#000;
            font-size:16px;
            width:30%;';

//create a new DOMDocument instance
$doc = new DOMDocument();
//loading a xml file
$doc->load('test.xml');
//searching for a specific tag
$names = $doc->getElementsByTagName('name');
echo '<center>
    <div style="' . $divStyle . '">
    <b><u>These are the elements with the tag \'name\':</u></b>
    <br /><br />';
foreach ($names as $name) {
    echo $name->nodeValue . '<br /><br />';
}
echo '</div>
    </center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).