4

我有一个 XML 提要,我可以在新闻网站上获取有关塞浦路斯的新闻。我想使用新闻图片,当然还有新闻本身。

这是一个xml示例

 <item>
      <title>Rumlardan KKTC üniversitelerine ambargo</title>
      <description>
        <![CDATA[<p><a href="http://www.ntvmsnbc.com/id/25368624/">
          <img align="left" border="0" src="http://media.ntvmsnbc.com/j/NTVMSNBC/Components/ArtAndPhoto-Fronts/Sections-StoryLevel/Dünya/Kıbrıs/120723girne.thumb.jpg" alt="" style="margin:0 5px 5px 0" /></a> 
          Kıbrıs Rum yönetimi, KKTC'deki üniversitelerle işbirliği yapan ülkelerin eğitim kurumlarına resmi yazılar göndererek yetkilileri tehdit ediyor. Hindistan üniversitesinden ortak akademik programlara son verilmesi istendi.</p><br clear="all" />]]>
      </description>
      <link>http://www.ntvmsnbc.com/id/25368624/</link>
      <media:content medium="image" url="http://media.ntvmsnbc.com/j/NTVMSNBC/Components/ArtAndPhoto-Fronts/Sections-StoryLevel/Dünya/Kıbrıs/120723girne.thumb.jpg">
        <media:text>
          <![CDATA[<p><a href="http://www.ntvmsnbc.com/id/25368624/">
            <img align="left" border="0" src="http://media.ntvmsnbc.com/j/NTVMSNBC/Components/ArtAndPhoto-Fronts/Sections-StoryLevel/Dünya/Kıbrıs/120723girne.thumb.jpg" alt="" style="margin:0 5px 5px 0" />
            </a>
            </p>
            <br clear="all" />]]>
        </media:text>
      </media:content>
      <pubDate>Mon, 23 Jul 2012 14:27:32 GMT</pubDate>
      <category>Haberler</category>
      <guid isPermaLink="true">http://www.ntvmsnbc.com/id/25368624/</guid>
    </item>

xml的链接是: http: //www.ntvmsnbc.com/id/24928068/device/rss/rss.xml

现在我使用下面的代码用 simpleXML 解析这个 xml。在此代码上,我可以防止显示<img>带有 strip_ 标签的标签,我只能在 CDATA 中显示文本数据。

简而言之,我要问的是如何media:content url=""进入我的代码,因为我想在那个 url 中将 thumb.jpg 更改为 hlarge.jpg 。

我试过$d['media'] = $news->media->attributes(); '<p>'.$post['media']['url'].了,但它不工作

这是我的代码:

<? 
$NewsFeedUrl =  "http://www.ntvmsnbc.com/id/24928068/device/rss/rss.xml";

$xml = @simplexml_load_file($NewsFeedUrl);

if(is_object($xml)){
    //Rest of our code will be here
}else{
    die('Güncel Haberlere Bağlanılamıyor.');
}

foreach($xml->channel->item as $news){
    if(is_array($newsContent) && count($newsContent)==$amountToShow){
    }
    $description = $news->description;
    $d['title'] =$news->title;
    $d['link'] = $news->link;
    $d['media'] = $news->media->attributes();
    $d['cont'] = $news->description;
    $d['date'] = $news->pubDate;
    $newsContent[]=$d;
}
//$ad=array("thumb", "left");

if(is_array($newsContent)){
    foreach($newsContent as $post){

        echo '

      <article class="entry"><h3>'.'<a href="'.$post['link'].' "target="_blank">'.$post['title'].'</a></h3>
      '.'<div class="meta"><span class="date_post">'.$post['date'].'</span>'.$post['pubDate'].
      //'<p>'.str_replace($ad,"hlarge",$post['cont']).
      '<p>'.$post['cont'].
      '<p>'.strip_tags($post['cont']).
      //'<p>'.$post['media']['url'].
      '<p><a href="'.$post['link'].' "target="_blank" class="button">Devamını Oku</a>'.
      ' </article>';
    }
}else{
    echo '<p>Güncel Haberler Alınamadı Sayfayı Yenilemeyi Deneyin.</p>';
}

?>
4

2 回答 2

3

content元素具有名称空间前缀 ( <media:content>),因此无法通过通常的方式访问它。

“媒体”的命名空间 URI 来自http://search.yahoo.com/mrss/(检查您rss.xml的“xmlns:media”)。

试试这个:

foreach ($xml->channel->item as $news)
{
    $ns_media = $news->children('http://search.yahoo.com/mrss/');

    echo $ns_media->content; // displays "<media:content>"
}

编辑 :

我认为命名空间uri“ http://search.yahoo.com/mrss/ ”存在一些问题

我尝试使用您的xml :

http://codepad.org/P90bOQUj [不工作]

我尝试使用其他xml :

http://codepad.org/ADYveL6T [工作]

于 2012-07-25T11:51:06.067 回答
0

我也遇到了这个问题,我用这个解决方案进行了测试:

https://gist.github.com/enderandpeter/6b760140bf9d2ed9620c

它奏效了!

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

$contentattr = $content->attributes();
于 2020-06-17T17:50:29.030 回答