how to access this child element - attribute in php simplexml

为君一笑 提交于 2019-12-12 23:04:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!