简体   繁体   中英

how to access this child element - attribute in php simplexml

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. ??

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM