Thursday, October 23, 2025
HomeLanguagesPHP | DOMNamedNodeMap item() function

PHP | DOMNamedNodeMap item() function

The DOMNamedNodeMap::item() function is an inbuilt function in PHP which is used to retrieve a node specified by index. This function is used to get a specific attribute from an element and further we can get the name or value of that attribute as per requirements.

Syntax:

DOMNamedNodeMap DOMNamedNodeMap::item( int $index )

Parameters:This function accepts a single parameter $index which holds the index.

Return Value: This function returns a node at the index’th position in the map.

Below given programs illustrate the DOMNamedNodeMap::item() function in PHP:
Program 1: In this example, we will get the name of attributes after fetching them using index()




<?php
// Create a new DOMDocument
$dom = new DOMDocument();
    
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
    <html>
        <h1 id=\"first\" 
            class=\"first\" attrib=\"attrib_value\"> Geeksforneveropen </h1>
    </html>
</root>");
    
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
    
// Get the 2nd attribute's value
$attribute1 = $node->attributes->item(0)->nodeName;
$attribute2 = $node->attributes->item(1)->nodeName;
$attribute3 = $node->attributes->item(2)->nodeName;
echo "$attribute1, $attribute2 and $attribute3";
?>


Output:

id, class and attrib

Program 2: In this example we will get the value of attributes after fetching them using index()




<?php
// Create a new DOMDocument
$dom = new DOMDocument();
    
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
    <html>
        <h1 id=\"first\" 
            class=\"neveropen\" attrib=\"attrib_value\"> Geeksforneveropen </h1>
    </html>
</root>");
    
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
    
// Get the 2nd attribute's value
$attribute1 = $node->attributes->item(0)->nodeValue;
$attribute2 = $node->attributes->item(1)->nodeValue;
$attribute3 = $node->attributes->item(2)->nodeValue;
echo "$attribute1, $attribute2 and $attribute3";
?>


Output:

first, neveropen and attrib_value

Reference: https://www.php.net/manual/en/domnamednodemap.item.php

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS