Here's a fresh answer, this is how to display all <item>
-nodes with the desired image-url(s).
Method #1: xpath()
:
$rss = simplexml_load_string($x);
$i = 1;
foreach ($rss->channel->item as $item) {
echo "#$i: $item->title" . PHP_EOL;
$i++;
// xpath
foreach ($item->xpath("//item[title = '$item->title']/media:content[media:title = 'Image']/@url") as $img)
echo "URL: $img" . PHP_EOL;
}
Comment: As xpath()
returns the values from the whole XML, I included the item/title
as condition to return only this item's children.
Method #2: iterating the "classic" way:
$rss = simplexml_load_string($x);
$i = 1;
foreach ($rss->channel->item as $item) {
echo "#$i: $item->title" . PHP_EOL;
$i++;
// classic
foreach ($item->children("media", TRUE) as $img) {
if ($img->title == 'Image') {
foreach ($img->attributes() as $name => $value)
if ($name == 'url') echo "URL: $value" . PHP_EOL;
}
}
}
see it working: https://eval.in/132862
The XML source is not valid, I added a >
in my example at eval.in