Forum Moderators: coopster

Message Too Old, No Replies

PHP / XML Parsing :( HELP!

Need help gettign attribute from nested tags in XML

         

webboy85

8:01 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Hi there,

I have an XML file with the following structure,

<item>
<name></name>
<description></description>
<Images>
<img src="" alt="" width="" height="" />
<img src="" alt="" width="" height="" />
<img src="" alt="" width="" height="" />
<img src="" alt="" width="" height="" />
</Images>
</item>


their are about 100 items but when i loop through each item and echo them onto the screen I only one to display one of the images that is listed..

Can anyone help me with the code to display just one?

I have tried:

$item->Images
$item->Images->img[1]->attributes()->src

but this does not seem to display anything and I know that using children() will just show a count of the children..

Im pulling my hair out and it seems like such a small problem

Thanks,
Chris

brotherhood of LAN

8:22 pm on Jul 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to the forums webboy


// $yourfile is the XML file
$dom = new DOMDocument;
$dom->loadHTML(file_get_contents($yourfile));
foreach($dom->getElementsByTagName('img') as $img)
{
$src = $img->getAttribute('src');
echo $src,"<br />";
}


Is that what you're after?

webboy85

8:32 pm on Jul 20, 2010 (gmt 0)

10+ Year Member



Ah sorry, I am using Simple_XML_Load_File to load the XML and parse it..

The code im using at the moment (that works to an extent is:

$inventorylink = 'inventory.xml';

$feed = simplexml_load_file($inventorylink);


// Display Results

foreach ($feed->item as $item) {

echo "<tr>";
echo"<td><strong><a href=\"\" title=\"\">$item->title</a></strong></td>";
echo "<td rowspan=\"2\" style=\"vertical-align:top; padding-bottom:15px;\">$item->Images->img[1]->attributes()->src</td>";
echo "</tr>";
echo "<tr>";
echo "<td><strong>Features:</strong> $item->description <br /><a href=\"\" title=\"More Details On The $item->title\">More Details</></td>";
echo "</tr>";

}

Any ideas?

Thank you for replying!