vendredi 13 mai 2011

How to Handle the 'CDATA Node' When using XMLReader

XMLReader is a breakthrough for processing large XML data as compared to the DOM reader which needs to load all the data in memory. This snippet prevents the read() function from returning a null value when the data is embedded in CDATA Nodes. The trick is to call the XMLReader::read() method until you escape the void string. More on XMLReader at http://www.php.net/manual/en/class.xmlreader.php

<?php

$products = array();  
$i=0;  
$xmlReader = new XMLReader();  
$xmlReader->open('cyril.xml');

while($xmlReader->read()) {  
    
 // check to ensure nodeType is an Element not attribute or #Text   
 if($xmlReader->nodeType == XMLReader::ELEMENT) {  

  //skip the first node which is the parent node for each product  
  $xmlReader->read();  

  //read all child nodes  
  while($xmlReader->read()){  
   $value = $xmlReader->value;  

   //if value was embeded in CDATA node then read this value  
   if($xmlReader->nodeType == XMLReader::CDATA){  
    $value = $xmlReader->value;  
    $xmlReader->read();  
   }  

   //end of product node - exit node  
   if($xmlReader->nodeType == XMLReader::END_ELEMENT && $xmlReader->localName == 'produit') {
    break;
   }

   //store your product data   
   $products[$i][$xmlReader->localName] = $value;  
  }
 }  
}  
    
$xmlReader->close();  
var_dump($products)

Hope this helps someone some day.

1 commentaire: