-2

I am parsing a xml and but there is a tag which contain image and text both and i want to seprate both image and text in diffrent columns of table in my design layout but i dont know how to do it. please help me. my php file is :

<?php
$RSS_Content = array();

function RSS_Tags($item, $type)
{
    $y = array();
    $tnl = $item->getElementsByTagName("title");
    $tnl = $tnl->item(0);
    $title = $tnl->firstChild->textContent;

    $tnl = $item->getElementsByTagName("link");
    $tnl = $tnl->item(0);
    $link = $tnl->firstChild->textContent;
    $tnl = $item->getElementsByTagName("description");
    $tnl = $tnl->item(0);
    $img = $tnl->firstChild->textContent;

    $y["title"]  = $title;
    $y["link"] = $link;
    $y["description"] = $img;
    $y["type"] = $type;

    return $y;
}

function RSS_Channel($channel)
{
    global $RSS_Content;

    $items = $channel->getElementsByTagName("item");

    // Processing channel

    $y = RSS_Tags($channel, 0);     // get description of channel, type 0
    array_push($RSS_Content, $y);

    // Processing articles

    foreach($items as $item)
    {
        $y = RSS_Tags($item, 1);    // get description of article, type 1
        array_push($RSS_Content, $y);
    }
}

function RSS_Retrieve($url)
{
    global $RSS_Content;

    $doc  = new DOMDocument();
    $doc->load($url);

    $channels = $doc->getElementsByTagName("channel");

    $RSS_Content = array();

    foreach($channels as $channel)
    {
        RSS_Channel($channel);
    }

}

function RSS_RetrieveLinks($url)
{
    global $RSS_Content;

    $doc  = new DOMDocument();
    $doc->load($url);

    $channels = $doc->getElementsByTagName("channel");

    $RSS_Content = array();

    foreach($channels as $channel)
    {
        $items = $channel->getElementsByTagName("item");
        foreach($items as $item)
        {
            $y = RSS_Tags($item, 1);
            array_push($RSS_Content, $y);
        }
    }

}

function RSS_Links($url, $size = 15)
{
    global $RSS_Content;

    $page = "<ul>";

    RSS_RetrieveLinks($url);
    if($size > 0)
    $recents = array_slice($RSS_Content, 0, $size + 1);

    foreach($recents as $article)
    {
        $type = $article["type"];
        if($type == 0) continue;
        $title = $article["title"];
        $link = $article["link"];
        $img = $article["description"];
        $page .= "<a href=\"#\">$title</a>\n";
    }

    $page .="</ul>\n";

    return $page;

}

function RSS_Display($url, $click, $size = 8, $site = 0, $withdate = 0)
{
    global $RSS_Content;

    $opened = false;
    $page = "";
    $site = (intval($site) == 0) ? 1 : 0;

    RSS_Retrieve($url);
    if($size > 0)
    $recents = array_slice($RSS_Content, $site, $size + 1 - $site);

    foreach($recents as $article)
    {
        $type = $article["type"];
        if($type == 0)
        {
            if($opened == true)
            {
                $page .="</ul>\n";
                $opened = false;
            }
            $page .="<b>";
        }
        else
        {
            if($opened == false)
            {
                $page .= "<table width='369' border='0'>
            <tr>";
                $opened = true;
            }
        }
        $title = $article["title"];
        $link = $article["link"];
        $img = $article["description"];
        $page .= "<td width='125' align='center' valign='middle'>
              <div align='center'>$img</div></td>                    
        <td width='228' align='left' valign='middle'><div align='left'><a 
                  href=\"$click\" target='_top'>$title</a></div></td>";
        if($withdate)
        {
            $date = $article["date"];
            $page .=' <span class="rssdate">'.$date.'</span>';
        }
            if($type==0)
            {
                $page .="<br />";
            }
        }

        if($opened == true)
        {
            $page .="</tr>
                </table>";
        }
        return $page."\n";

    }
?>
Smile
  • 2,770
  • 4
  • 35
  • 57
  • 2
    What have you already tried, and where did it break? And show us your XML. – nl-x May 15 '13 at 07:19
  • http://ibnlive.in.com/ibnrss/top.xml that is my xml – user2130861 May 15 '13 at 07:53
  • and i want to break $img = $article["description"]; this tag – user2130861 May 15 '13 at 07:53
  • 1
    Some tips for asking a question on Stackoverflow: 1.) Properly indent your code. 2.) As common in debugging it should be also common when you ask a question here that you have PHP's error reporting set to the highest level and you log errors. Take care code you paste here is Warning and Notices free. See [How to get useful error messages in PHP?](http://stackoverflow.com/q/845021/367456) and [PHP: How do I enable error reporting?](http://stackoverflow.com/q/6575482/367456) 3.) Ask an actual programming question, its not clear into which *concrete* problem you run here to program what you want. – hakre May 15 '13 at 10:03

1 Answers1

0

To separate the image and description you need to parse the HTML that is stored inside the description element again as XML. Luckily it is valid XML inside that element, therefore you can do this straight forward with SimpleXML, the following code-example take the URL and converts each item *description* into the text only and extracts the src attribute of the image to store it as the image element:

<item>
    <title>Fake encounter: BJP backs Kataria, says CBI targeting Modi</title>
    <link>http://ibnlive.in.com/news/fake-encounter-bjp-backs-kataria-says-cbi-targeting-modi/391802-37-64.html</link>
    <description>The BJP lashed out at the CBI and questioned its 'shoddy investigation' into the Sohrabuddin fake encounter case.</description>
    <pubDate>Wed, 15 May 2013 13:48:56 +0530</pubDate>
    <guid>http://ibnlive.in.com/news/fake-encounter-bjp-backs-kataria-says-cbi-targeting-modi/391802-37-64.html</guid>
    <image>http://static.ibnlive.in.com/ibnlive/pix/sitepix/05_2013/bjplive_kataria3.jpg</image>
</item>

The code-example is:

$url  = 'http://ibnlive.in.com/ibnrss/top.xml';
$feed = simplexml_load_file($url);

$items = $feed->xpath('(//channel/item)');

foreach ($items as $item) {
    list($description, $image) =
        simplexml_load_string("<r>$item->description</r>")
            ->xpath('(/r|/r//@src)');
    $item->description = (string)$description;
    $item->image       = (string)$image;
}

You can then import the SimpleXML into a DOMElement with dom_import_simplexml() however honestly, I just would wrap that little HTML creation as well into a foreach of SimpleXML because you can make use of LimitIterator for the paging as well as you could with DOMDocument and the data you access is actually easily at hand with SimpleXML, it's just easy to pass along the XML elements as SimpleXMLElements instead of parsing into an array first and then processing the array. That's moot.

hakre
  • 193,403
  • 52
  • 435
  • 836