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条回答 默认 最新

  • dq201401 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);
        }
    
     ?>
    
    打赏 评论

相关推荐 更多相似问题