how to read only part of an xml file with php xmlreader -


i have rss xml file pretty large, more 700 nodes. using xmlreader iterator library parse , display results 10 per page.

this sample code parsing xml:

<?php require('xmlreader-iterators.php');  $xmlfile = 'http://www.example.com/rss.xml'; $reader = new xmlreader(); $reader->open($xmlfile);  $itemiterator = new xmlelementiterator($reader, 'item'); $items = array();  foreach ($itemiterator $item) {     $xml     = $item->assimplexml();     $items[] = array(         'title'     => (string)$xml->title,         'link'      => (string)$xml->link     ); }  // logic displaying array values, based on current page.  // page = 1 means $items[0] $items[9]  for($i = 0; $i <= 9; $i++) {            echo '<a href="'.$items[$i]['link'].'">'.$items[$i]['title'].'</a><br>';       } ?> 

but problem that, every page, parsing entire xml file , displaying corresponding page results, like: if page 1, displaying 1 10 nodes, , if page 5, displaying 41 50 nodes.

it causing delay in displaying data. possible read nodes corresponding requested page? first page, can read nodes 1 10 positions, instead of parsing xml file , display first 10 nodes. in other words, can apply limit while parsing xml file?

i came across this answer of gordon addresses similar question, using simplexml, not recommended parsing large xml files.

use array_splice extract portion of array

require ('xmlreader-iterators.php');  $xmlfile = 'http://www.example.com/rss.xml'; $reader = new xmlreader(); $reader->open($xmlfile);  $itemiterator = new xmlelementiterator($reader, 'item'); $items = array();  $curr_page = (0 === (int) $_get['page']) ? 1 : $_get['page'];  $pages = 0;  $max = 10;  foreach ($itemiterator $item) {    $xml = $item->assimplexml();    $items[] = array(        'title' => (string) $xml->title,        'link' => (string) $xml->link   ); }  // take length of array $len = count($items);  // number of pages  $pages = ceil($len / $max);  // calculate starting point $start = ceil(($curr_page - 1) * $max);  // return portion of results $arrayitem = array_slice($items, $start, $max);  ($i = 0; $i <= 9; $i ++) {     echo '<a href="' . $arrayitem[$i]['link'] . '">' . $arrayitem[$i]['title'] . '</a><br>';  }   // pagining stuff   ($i = 1; $i <= $pages; $i ++) {     if ($i === (int) $page) {        // current page         $str[] = sprintf('<span style="color:red">%d</span>', $i);    } else {        $str[] = sprintf('<a href="?page=%d" style="color:green">%d</a>', $i, $i);   } }   echo implode('', $str); 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -