This section contains the detail about the Get Attribute From Existing File in xml using PHP.
PHP-XML Get Attribute From Existing File:
In this example, we will get attribute name and value in the existing xml file.
First we will create a dom document object and load xml document like:
<?php $xmldoc = new DomDocument; $xmldoc->Load('root.xml');?>
Then we will used getElementByTagName() function to find out a particular element and used getAttributeNode() function to get attribute node like:
$chield1 = $xmldoc->getElementsByTagName('child1')->item(0); $node = $chield1->getAttributeNode('chield1_attribute');
After that print the attribute name and value.
Example:
<?php $xmldoc = new DomDocument; $xmldoc->Load('root.xml'); $chield1 = $xmldoc->getElementsByTagName('child1')->item(0); $node = $chield1->getAttributeNode('chield1_attribute'); echo "<html>"; echo "<head>"; echo "<title>Get Attribute from XML file and attribute value</title>"; echo "</head>"; echo "<body>"; echo "<b>Attribute Name is :".$node->name; echo "<br>Attribute Value is :".$node->value; echo "</b></body>"; echo "</HTML>"; ?>
and root.xml code is:
<?xml version="1.0"?>
<root>
<child1 chield1_attribute="chield1 attribute value">clied1 text</child1>
<child2 name="">clied2 text</child2>
</root>
After running the example output is:
Browser Output:
Attribute Name is :chield1_attribute
Attribute Value is :chield1 attribute value
[ 0 ] Comments