问题
I want to access the 'url' attribute in the media:content element below. I am pretty sure this gives me the media:content, but I can not seem to 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 tried variations:
$theURL = $item->children('media', true)->content['url'];
and
$mediaItem=$item->children('media', true)->content;
$contentItem=$mediaItem->children('content', true);
$url = $contentItem['url'];
No luck. ??
回答1:
Complete 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
?>
References: SimpleXML Attributes
来源:https://stackoverflow.com/questions/3575516/how-to-access-this-child-element-attribute-in-php-simplexml