Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/04/2010, 11:30
Avatar de neodani
neodani
 
Fecha de Ingreso: marzo-2007
Mensajes: 1.811
Antigüedad: 17 años, 1 mes
Puntos: 20
Que es mas rapido para procesar un fichero XML?

Buenas,

Hasta ahora para procesar grandes ficheros XML primero los traducia a un array y luego los iteraba.

Para pasar un fichero XML a un array utilizo la siguiente clase

Código PHP:
Ver original
  1. <?php
  2. class xml {
  3.     var $matriz = array();
  4.     var $resultado;
  5.     var $informacion;
  6.  
  7.     function xml($contenido) {
  8.  
  9.             $this->resultado = xml_parser_create ();
  10.             xml_set_object($this->resultado,$this);
  11.             xml_set_element_handler($this->resultado, "abrir", "cerrar");
  12.            
  13.             xml_set_character_data_handler($this->resultado, "info");
  14.        
  15.             $this->informacion = xml_parse($this->resultado,$contenido);
  16.                          
  17.             xml_parser_free($this->resultado);
  18.            
  19.             return $this->matriz;
  20.     }
  21.     function abrir($parser, $nombre, $atributos) {
  22.         $etiqueta = array("nombre"=>$nombre,"atributos"=>$atributos);
  23.         array_push($this->matriz,$etiqueta);
  24.     }
  25.  
  26.     function info($parser, $etiqueta_info) {
  27.         if(trim($etiqueta_info)) {
  28.             if(isset($this->matriz[count($this->matriz)-1]['info'])) {
  29.                $this->matriz[count($this->matriz)-1]['info'] .= $etiqueta_info;
  30.             }
  31.             else {
  32.                $this->matriz[count($this->matriz)-1]['info'] = $etiqueta_info;
  33.             }
  34.         }
  35.     }
  36.     function cerrar($parser, $nombre) {
  37.        $this->matriz[count($this->matriz)-2]['hijo'][] = $this->matriz[count($this->matriz)-1];
  38.         array_pop($this->matriz);
  39.     }
  40. }
  41. ?>

Y la llamada es así

Código PHP:
Ver original
  1. $xml_contenido = @file_get_contents($url,'r');
  2. $xml = new xml($xml_contenido);
  3. $arreglo = ($xml->matriz);

Luego voy iterando con bucles anidados buscando la información que quiero.

Me pregunto si alguien que haya tenido experiencia con este método se ha dado cuenta si es más rápido o no que utilizar alguna clase como SimpleXML o similares.

Vuestra recomendación?

Muchas gracias de antemano!