Parse XML with php
Times may come when you want to parse an XML page with php .. or read feeds with php. “So, how to do it ?” .. is the question..
Here’s it:
$fileURL = “./fileName.xml”; //Specify the path(or Link of the xml file. )
$string = file_get_contents($fileURL); //This will load the wole content of xml file into the variable.
$xml = simplexml_load_string($string); //simplexml_load_string, loads the data inform of an object or returns false if there is a problem in xml
if($xml !== false) //checks weather xml was sucessfully loaded
{
//now suppose we want to list down all the attributes of tag FIRST ModulePrefs tag i.e <ModulePrefs …. ></…>
foreach($xml->ModulePrefs[0]->attributes() as $a => $b) \\ [0] means the first ModulePrefs tag
{
echo $a = $b \\here $a is the attribute and $b is the value.
}
}
Easy isn’t it?

