How to get the value of an attribute from XML file in PHP?

You should be able to get this using SimpleXMLElement::attributes()

Try this:

$xml=simplexml_load_file($file);
foreach($xml->Var[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}

That will show you all the name/value attributes for the first foo element. It’s an associative array, so you can do this as well:

$attr = $xml->Var[0]->attributes();
echo $attr['VarNum'];

Leave a Comment