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


$products = array();  
$xmlReader = new XMLReader();  

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  

  //read all child nodes  
   $value = $xmlReader->value;  

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

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

   //store your product data   
   $products[$i][$xmlReader->localName] = $value;  

Hope this helps someone some day.

1 commentaire: