I want to read nested elements and each element's attribute values from an XML file. I am using simpleXML and here is my code so far:
$path = Mage::getBaseUrl('media').'Banners/data.xml' ;
$doc = new DOMDocument();
$doc->load( $path );
$items = $doc->getElementsByTagName( "imgitem" );
$xml=simplexml_load_file($path);
foreach($xml->children() as $child)
{
foreach( $child as $item )
{
$attr = $xml->imgitem->attributes();
echo $attr['image'].'<br>';
echo $attr['link'].'<br>';
echo $attr['target'].'<br>';
}
}
Can anyone point me in the right direction in order to get all the child nodes and their attributes recursively?
Here is the source XML file structure
<Banner>
<imgitem image="img/source/path" link="" target="_blank" delay="" textBlend="yes"> <![CDATA[Autumn Leaves]]> </imgitem>
<imgitem image="img/source/path" link="" target="_blank" delay="" textBlend="yes"> <![CDATA[Creek]]> </imgitem>
<imgitem image="img/source/path" link="" target="_blank" delay="" textBlend="yes"> <![CDATA[Dock]]> </imgitem>
</Banner>
Now what I want is the values of all the attributes of each of the three items of the parent tag. So need to loop through the main tag to get all three (or any number of) items and then take another loop and fetch all the attribute values of each item.