How to read xml value with specific attribute


 

php getElementsByTagName with specific attribute

Read xml value with specific attribute

XML format is :

File Name = locallang_db.xml

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3locallang>
    <meta type="array">
        <type>database</type>
        <description>Description..</description>
    </meta>
    <data type="array">
        <languageKey index="default" type="array">
            <label index="name">Jainish</label>
            <label index="surname">Senjaliya</label>
            <label index="dob">19-11-19XX</label>
            <label index="age">Top Secret</label>
        </languageKey>
    </data>
</T3locallang>	

Read XML File from PHP

$xmlPath = 'locallang_db.xml';
$objDOM = new DOMDocument('1.0', 'utf-8');
$objDOM->load($xmlPath); //make sure path is correct 
$label = $objDOM->getElementsByTagName("label"); 

foreach ($label as $val)
{
    if($val->getAttribute('index') === 'name')
    {
        echo  $val->nodeValue;
        // do something
    }
 // You can also get tag name by : $val->tagName OR $val->nodeName;
}		

OutPut :

 Jainish

Best Luck 🙂

If you have any query then feel free to contact me at Jainish Senjaliya

Advertisements

One thought on “How to read xml value with specific attribute

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s