dtest84004 2015-05-29 14:06
浏览 31
已采纳

如何在php中使用rss时访问元素的属性

I am using a RSS-feed to display some news. But the thing is that i use bootstrap and the RSS-feed send a image that i cant figure out how to access, so i can putt class="img-responsive" on it. Now the images dosent fits to the screen..

php code when i collect the elments from the RSS-feed:

<?php
    $q=$_GET["q"];

    $xml=("https://www.gamereactor.se/rss/rss.php?texttype=4");

    $xmlDoc = new DOMDocument();
    $xmlDoc->load($xml);

    $channel=$xmlDoc->getElementsByTagName('channel')->item(0);
    $channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
    $channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
    $channel_desc = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;

    echo("<br>");
    echo($channel_desc."</p>");

    $x=$xmlDoc->getElementsByTagName('item');
    for ($i=0; $i<=2; $i++) {$item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
    $item_link=$x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
    $item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
    echo ("<p><a href='".$item_link."'>".$item_title."</a>");
    echo ("<br>");
    echo ($item_desc);
    echo ("<img class=img-responsive>".$item_desc."<img></p>");} //where im trying to access the image..

 ?>

This how the xml from the RSS-feed looks:

<title>Nya Need for Speed kommer att kräva internetuppkoppling</title>
<description>&lt;img src=&quot;http://www.gamereactor.dk/media/55/_1455534.jpg&quot;&gt;&lt;p&gt;Svenska Ghost Games pillar för fullt med kommande Need for Speed som ska släppas till PC, Playstation 4 och Xbox One den 3:e november. Förutom att vi tidigare rapporterat...&lt;/p&gt;</description>
  • 写回答

1条回答 默认 最新

  • 普通网友 2015-05-29 18:09
    关注

    The issue is with the data you get back. When you view the XML, you will see:

    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media= "http://search.yahoo.com/mrss/">
    <channel>
        <atom:link href="http://www.gamereactor.se/rss/rss.php?texttype=4" rel="self" type="application/rss+xml" />
        <title>Gamereactor Sverige Nyheter</title>
        <link>http://www.gamereactor.se</link>
        <description>Dagsfärska nyheter, tunga artiklar, intervjuer, personliga bloggar, intressanta forumdiskussioner och rättvisa recensioner. Gamereactor är Sveriges största speltidning- och spelcommunity</description>
        <webMaster>kenneth.gant@gamereactor.dk (Kenneth Gant)</webMaster>
        <item>
            <link>http://www.gamereactor.se/nyheter/244164/Alla+f%F6rhandsbokningsbonusar+till+Arkham+Knight/?rs=rss</link>
            <guid>http://www.gamereactor.se/nyheter/244164/?rs=rss</guid>
            <title>Alla förhandsbokningsbonusar till Arkham Knight</title>
            <description>&lt;img src=&quot;http://www.gamereactor.dk/media/28/framtidensbatman_1102834.jpg&quot;&gt;&lt;p&gt;En flitig Reddit-besökare har tagit på sig att lista var och till vilket format du bör reservera Batman: Arkham Knight för att få den förhandsbokningsbonus just du vill...&lt;/p&gt;</description>
            <author>jonas@gamereactor.se (Jonas Mäki)</author>
            <pubDate>Fri, 29 May 2015 19:44:54 +0200</pubDate>
        </item>
    

    So the IMG tag is within the description and encoded. So the following line will have this detail:

    $item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

    To make use of this as HTML in your page, we want to do:

    $item_desc=html_entity_decode($x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue);

    $item_desc will now contain: <img src="http://www.gamereactor.dk/media/28/framtidensbatman_1102834.jpg"><p>En flitig Reddit-besökare har tagit på sig att lista var och till vilket format du bör reservera Batman: Arkham Knight för att få den förhandsbokningsbonus just du vill...</p>

    <?php
        $q=$_GET["q"];
    
        $xml=("https://www.gamereactor.se/rss/rss.php?texttype=4");
    
        $xmlDoc = new DOMDocument();
        $xmlDoc->load($xml);
    
        $channel=$xmlDoc->getElementsByTagName('channel')->item(0);
        $channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
        $channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
        $channel_desc = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
    
        echo("<br>");
        echo($channel_desc."</p>");
    
        $x=$xmlDoc->getElementsByTagName('item');
        for ($i=0; $i<=2; $i++){
            $item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
            $item_link=$x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
            $item_desc=html_entity_decode($x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue);
            echo ("<p><a href='".$item_link."'>".$item_title."</a>");
            echo ("<br>");
            echo ($item_desc);
        }
    
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?