XML parsing in php -
i parsing xml , there tag contain image , text both , want seprate both image , text in diffrent columns of table in design layout dont know how it. please me. php file :
<?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); // description of channel, type 0 array_push($rss_content, $y); // processing articles foreach($items $item) { $y = rss_tags($item, 1); // 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 $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 $channel) { $items = $channel->getelementsbytagname("item"); foreach($items $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 $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 $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"; } ?>
to separate image , description need parse html stored inside description element again xml. luckily valid xml inside element, therefore can straight forward simplexml, following code-example take url , converts each item *description* text , extracts src attribute of image store 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 @ cbi , questioned 'shoddy investigation' 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 $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 import simplexml domelement dom_import_simplexml()
honestly, wrap little html creation foreach of simplexml because can make use of limititerator
paging could domdocument , data access @ hand simplexml, it's easy pass along xml elements simplexmlelements instead of parsing array first , processing array. that's moot.
Comments
Post a Comment