Ver Mensaje Individual
  #2 (permalink)  
Antiguo 22/09/2006, 03:50
Koden
 
Fecha de Ingreso: marzo-2005
Mensajes: 197
Antigüedad: 19 años, 1 mes
Puntos: 1
Yo para parsear archivos uso esta funcion... Realmente no recuerdo de qué web la saqué (aunque era en castellano y lo explicaba bastante bien) pero igualmente te dejo la función, a ver si te vale... Después sólo tienes que llamarla en un include y crear un nuevo objeto que maneje la información.

Código PHP:
<?php
class parser_rss
{
  var 
$titulo;
  var 
$descripcion;
  var 
$link;
  var 
$imagen;
  var 
$items;
  var 
$parser;
  var 
$rss;
  var 
$tag_actual;
  var 
$contenido;
  var 
$en_canal;
  var 
$en_imagen;
  var 
$en_item;
  var 
$item;
  var 
$error;

  function 
class_rss()
  {
    
$this->titulo "";
    
$this->descripcion "";
    
$this->link "";
    
$this->imagen = Array();
    
$this->items = Array();
    
$this->tag_actual "";
    
$this->contenido "";
    
$this->en_canal false;
    
$this->en_imagen false;
    
$this->en_item false;
    
$this->item 0;
    
$this->error "";

    return 
true;
  }

  function 
parsear_archivo($archivo)
  {
    if(!(
$fp fopen($archivo'r')))
    {
      
$this->error "Imposible abrir el archivo:" $archivo "<br>";
      return 
false;
    }

    
$this->rss "";
    while(!
feof($fp))
    {
      
$this->rss .= fread($fp4096);
    }
    
fclose($fp);

    
$this->parser xml_parser_create();

    
xml_set_object($this->parser, &$this);

    
xml_set_element_handler($this->parser,"tag_abre","tag_cierra");
    
xml_set_character_data_handler($this->parser,"tag_contenido");

    if(!
xml_parse($this->parser$this->rsstrue))
    {
      
$this->error "Error en la linea " xml_get_current_line_number($this->parser) . ": " xml_error_string(xml_get_error_code($this->parser)) . "<br>En el archivo: " $archivo;
      return 
false;
    }

    
xml_parser_free($this->parser);

    return 
true;
  }

  function 
tag_abre($parser$nombre$atributos)
  {
    
$this->tag_actual $nombre;

    switch(
$this->tag_actual)
    {
      case 
"CHANNEL":
        
$this->en_canal true;
        
$this->en_imagen false;
        
$this->en_item false;
        break;
      case 
"IMAGE":
        
$this->en_imagen true;
        
$this->en_canal false;
        break;
      case 
"ITEM":
        
$this->en_item true;
        
$this->en_canal false;
        break;
    }
  }

  function 
tag_contenido($parser$contenido)
  {
    
$this->contenido .= $contenido;
  }

  function 
tag_cierra($parser$nombre)
  {
    
$this->contenido trim($this->contenido);

    switch(
$nombre)
    {
      case 
"TITLE":
        if(
$this->en_canal)
        {
          
$this->titulo $this->contenido;
        }
        elseif(
$this->en_item)
        {
          
$this->items[$this->item]['titulo'] = $this->contenido;
        }
        elseif(
$this->en_imagen)
        {
          
$this->imagen['titulo'] = $this->contenido;
        }
        break;
      case 
"DESCRIPTION":
        if(
$this->en_canal)
        {
          
$this->descripcion $this->contenido;
        }
        elseif(
$this->en_item)
        {
          
$this->items[$this->item]['descripcion'] = $this->contenido;
        }
        break;
      case 
"LINK":
        if(
$this->en_canal)
        {
          
$this->link $this->contenido;
        }
        elseif(
$this->en_item)
        {
          
$this->items[$this->item]['link'] = $this->contenido;
        }
        elseif(
$this->en_imagen)
        {
          
$this->imagen['link'] = $this->contenido;
        }
        break;
      case 
"URL":
        if(
$this->en_imagen)
        {
          
$this->imagen['url'] = $this->contenido;
    }
        break;
      case 
"CHANNEL":
        
$this->en_canal false;
        break;
      case 
"IMAGE":
        
$this->en_imagen false;
        break;
      case 
"ITEM":
        
$this->en_item false;
        
$this->item++;
        break;
    }

    
$this->contenido "";
    
$this->tag_actual "";
  }

  function 
retorno_a()
  {
    return 
$this->titulo;
  }

  function 
retorno_b()
  {
    return 
$this->link;
  }
}
?>