我想获取图片的网址。 XML文档树如下:
<rss xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>
<![CDATA[ The Star Online Business Highlights ]]>
</title>
<link>/TheStar/Website</link>
<description>...</description>
<image>...</image>
<language>en</language>
<item>
<guid isPermaLink="false">{F88B27DD-24FB-4807-941F-070D772B7586}</guid>
<link>
http://www.thestar.com.my/business/business-news/2017/10/24/top-glove-says-not-buying-adventa-nor-supermax/
</link>
<title>
<![CDATA[ Top Glove says not buying Adventa nor Supermax ]]>
</title>
<description>
<![CDATA[KUALA LUMPUR: Top Glove, which has allocated about RM1bil to expand via mergers, has denied news reports the target companies are Adventa Bhd and Supermax Corporation Bhd.]]>
</description>
<pubDate>Tue, 24 Oct 2017 13:17:18 +08:00</pubDate>
<enclosure url="http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1&w=0&h=0&" length="" type="image/jpeg"/>
<media:content url="http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1&w=0&h=0&" type="image/jpeg">
<media:description>
<![CDATA[ ]]>
</media:description>
</media:content>
<section>
<![CDATA[ Business ]]>
</section>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
</channel>
由于有多个项目,我想让它成为一个循环,我尝试了:
foreach($xml->channel->item as $news) {
$media = $news->media->children('http://search.yahoo.com/mrss/');
echo ($media->content);
}
以及
foreach($xml->channel->item as $news) {
$media = $news->children('http://search.yahoo.com/mrss/');
echo ($media->content);
}
但两者似乎都失败了。什么是正确的方法?
答案 0 :(得分:0)
inputs_layer
答案 1 :(得分:0)
$media
变量的类型为SimpleXMLElement。
您可以做的是将$media
变量循环到foreach中,然后从attributes获取url
。
例如(使用simplexml_load_string和additional Libxml parameters加载示例xml:
$source = <<<SOURCE
//Your example xml here
SOURCE;
$xml = simplexml_load_string($source, "SimpleXMLElement", LIBXML_NOERROR|LIBXML_ERR_NONE|LIBXML_ERR_FATAL);
foreach($xml->channel->item as $news) {
$media = $news->children('http://search.yahoo.com/mrss/');
foreach($media as $child) {
echo $child->attributes()->url;
}
}
将导致:
http://www.thestar.com.my/~/media/online/2017/08/22/03/58/hartalega-glove3.ashx?crop=1=0=0