How to access this child - attribute in php simplexml

I want to access the url attribute in the media: content element below. I am sure that this gives me media: content, but I cannot get the URL (see What I tried below):

$theContent = $item->children('media', true)->content;

XML

<item>
<media:content type="image/jpeg" url="my url" />
</item>

I have verified options :

 $theURL = $item->children('media', true)->content['url'];

and

$mediaItem=$item->children('media', true)->content;
$contentItem=$mediaItem->children('content', true);
$url = $contentItem['url'];

Bad luck.??

+1
source share
1 answer

Full working code with output:

<?php
$xml = '<item>
<media:content type="image/jpeg" url="my url" />
</item>';

$theContent = @new SimpleXMLElement($xml);

$attributes = $theContent->content->attributes();
echo $attributes['url'];  //outputs: my url
?>

Links: SimpleXML Attributes

+1
source

All Articles