php - Get XML code count using XMLReader -


i trying parse product feed provided google merchant. thing want more interactive using function convert xml array , show percentage user how products updated. have read xmlreader more efficient other parsing techniques.
how can make xmlreader more effective. can number of nodes using xmlreader. or how can iterate on xml can more responsive.

converting xml array wrong idea. mean build data structure in memory. have data structure, converting array mean loose data , features. read xml directly , use it.

here several ways archive want. if feed small can use dom directly. allows use xpaths count() function.

the google product-feed bases on rss 2.0 or atom 1.0. atom better format let's use that.

// create dom document , load xml  $dom = new domdocument(); $dom->loadxml($xml);  // create xpath object , register prefixes 2 namespaces $xpath = new domxpath($dom); $xpath->registernamespace('atom', 'http://www.w3.org/2005/atom'); $xpath->registernamespace('gi', 'http://base.google.com/ns/1.0');  // output entry count var_dump($xpath->evaluate('count(//atom:entry)'));  // iterate entries foreach ($xpath->evaluate('//atom:entry') $entry) {   // output data them   var_dump(     [       'title' => $xpath->evaluate('string(atom:title)', $entry),       'summary' => $xpath->evaluate('string(atom:summary)', $entry),       'image-link' => $xpath->evaluate('string(gi:image_link)', $entry)     ]   ); } 

if product feed large, loading memory might not working. count have load them memory or iterate them twice. 1 possible approach around file size. not exact progress of course. should enough.

$file = 'feed.xml'; $filesize = filesize('feed.xml'); $readbytes = 0;  // xml reader file $reader = new xmlreader; $reader->open($file);  // xml document, xpath , register namespaces $dom = new domdocument(); $xpath = new domxpath($dom); $xpath->registernamespace('atom', 'http://www.w3.org/2005/atom'); $xpath->registernamespace('gi', 'http://base.google.com/ns/1.0');  // first entry element while ($reader->read() && $reader->localname !== 'entry') {   continue; }  // while have entry element while ($reader->localname === 'entry') {   // import entry prepared document   $entry = $reader->expand($dom);   var_dump(     [       'title' => $xpath->evaluate('string(atom:title)', $entry),       'summary' => $xpath->evaluate('string(atom:summary)', $entry),       'image-link' => $xpath->evaluate('string(gi:image_link)', $entry)     ]   );    $readbytes += strlen($reader->readouterxml());   printf(     'read %s of %s bytes, %d%%',     $readbytes,     $filesize,     round($readbytes * 100 / $filesize)   );    // move next entry sibling   $reader->next('entry'); } 

be aware using xml reader slower. calculating status cost performance, too. might better idea show how many entries have been read.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -