PHP | DOMElement getAttributeNode() Function Last Updated : 26 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMElement::getAttributeNode() function is an inbuilt function in PHP which is used to get the attribute node with name, for the current element. Syntax: DOMAttr DOMElement::getAttributeNode( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the attribute. Return Value: This function returns an DOMAttr value containing the attribute node. Below examples illustrate the DOMElement::getAttributeNode() function in PHP: Example 1: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <body> <div attr=\"value\"> DIV 1 </div> </body>"); // Get the elements by tagname $elements = $dom->getElementsByTagName('div'); // Get the attribute node $node = $elements[0]->getAttributeNode('attr'); // Extract name $name = $node->name; // Extract value $value = $node->value; echo $name . " => " . $value . "<br>"; ?> Output: attr => value Example 2: php <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <body> <div id=\"div1\"> DIV 1 </div> <div id=\"div2\"> DIV 2 </div> <div id=\"div3\"> DIV 3 </div> </body>"); // Get the elements by tagname $elements = $dom->getElementsByTagName('div'); // Get the id of a element echo "All the divs with id values are: <br>"; foreach ($elements as $element) { // Get the attribute node $node = $element->getAttributeNode('id'); // Extract name $name = $node->name; // Extract value $value = $node->value; echo $name . " => " . $value . "<br>"; } ?> Output: All the divs with id values are: id => div1 id => div2 id => div3 Reference: https://www.php.net/manual/en/domelement.getattributenode.php Comment More infoAdvertise with us Next Article PHP | DOMElement getAttributeNode() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMElement getAttributeNodeNS() Function The DOMElement::getAttributeNodeNS() function is an inbuilt function in PHP which is used to get the attribute node in specific namespace with local name for the current node. Syntax: DOMAttr DOMElement::getAttributeNodeNS( string $namespaceURI, string $localName ) Parameters: This function accepts 2 min read PHP | DOMElement getAttributeNS() Function The DOMElement::getAttributeNS() function is an inbuilt function in PHP which is used to get the value of the attribute in a specific namespace with local name for the current node. Syntax: string DOMElement::getAttributeNS( string $namespaceURI, string $localName ) Parameters: This function accepts 2 min read PHP | DOMElement getAttribute() Function The DOMElement::getAttribute() function is an inbuilt function in PHP which is used to get the value of the attribute with name for the current node. Syntax: string DOMElement::getAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the attribu 1 min read PHP | DOMElement setAttributeNode() Function The DOMElement::setAttributeNode() function is an inbuilt function in PHP which is used to add a new attribute node to element. Syntax: DOMAttr DOMElement::setAttributeNode( DOMAttr $attr ) Parameters: This function accepts a single parameter $attr which holds the attribute node to be added. Return 2 min read PHP | DOMElement hasAttribute() Function The DOMElement::hasAttribute() function is an inbuilt function in PHP which is used to know whether attribute with a specific name exists as a member of the element. Syntax: bool DOMElement::hasAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name 1 min read Like