PHP | DOMElement getAttributeNode() Function
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
$dom = new DOMDocument();
$dom ->loadXML("<?xml version=\"1.0\"?>
<body>
<div attr=\"value\"> DIV 1 </div>
</body>");
$elements = $dom ->getElementsByTagName( 'div' );
$node = $elements [0]->getAttributeNode( 'attr' );
$name = $node ->name;
$value = $node ->value;
echo $name . " => " . $value . "<br>" ;
?>
|
Output:
attr => value
Example 2:
<?php
$dom = new DOMDocument();
$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>");
$elements = $dom ->getElementsByTagName( 'div' );
echo "All the divs with id values are: <br>" ;
foreach ( $elements as $element ) {
$node = $element ->getAttributeNode( 'id' );
$name = $node ->name;
$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
Last Updated :
26 Feb, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...