Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Parse/Get XML attribute using PHP SimpleXMLElement

Hello Friends,

Today I write something about XML parsing using PHP. I am taking XML file contents using file_get_contents function. Now parsing file contents by creating object of SimpleXMLElement. I can easily get all elements of xml using $xml->name or if it is an array than $xml->name[0]->fname. But the problem is with attributes. I have one array which have attributes. Below is the simple way to get/parse XML attributes.

$url = file_get_contents("data.xml");
$xml = new SimpleXMLElement($url);
foreach($xml->attrarray[$i]->attributes() as $val)
{
	echo $val;
}

Let me know if you find any problem.

Thank You!

Share:

3 Comments

Leave a Reply

Your email address will not be published.