The
<?php
$xmlstr = '<?xml version="1.0" standalone="yes"?>
<table count="" time="0.010006904602051">
<item>
<id>607</id>
<name>MSPOT6071</name>
<description>Hip Hop / Raps</description>
<type>3</type>
<radio_folder_id/>
<albumart>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_175.jpg
</albumart>
<albumart_300>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_350.jpg
</albumart_300>
<albumart_500>
http://cdn.7static.com/static/img/sleeveart/00/009/560/0000956027_500.jpg
</albumart_500>
</item>
<item>
<id>48542614</id>
<name>US Pop - TESTB</name>
<description>Blues</description>
<type>3</type>
<radio_folder_id/>
</item>
</table>';
$xml = new SimpleXMLElement($xmlstr);
foreach($xml->item as $item)
{
echo $item->name."<br>";
}
echo $xml->item[0]->name;
echo '<pre>';
print_r($xml);
echo '</pre>';
?>
Assign your XML string to a variable $xmlstr and so you don't run into incomplete XML document errors make sure you include the following at the top of your XML document.
<?xml version="1.0" standalone="yes"?>
Then use the built-in SimpleXML class by passing your XML string $xmlstr to SimpleXML:
$xml = new SimpleXMLElement($xmlstr);
Now you can access your XML file as a PHP object using the attributes and methods of the SimpleXML class. In this example we loop over the 'item'(s) in your XML document and print out the 'name' elements:
foreach($xml->item as $item)
{
echo $item->name."<br>";
}
I have also include code to access the first item element:
echo $xml->item[0]->name;
As well as some debugging code view the XML document in a SimpleXML object:
echo '<pre>';
print_r($xml);
echo '</pre>';
You access the key or in this case the object property by its name. So in your foreach loop you might do:
if($item->name)
{
echo $item->name;
}
or
if($item->description)
{
echo $item->description;
}
May the force be with you.