Foros del Web » Programando para Internet » PHP »

Parsear XML con PHP

Estas en el tema de Parsear XML con PHP en el foro de PHP en Foros del Web. Pues eso, me gustaría que me comentáseis alguna buena forma de parsear un archivo XML para poder utilizarlo en PHP. He utilizado el buscador de ...
  #1 (permalink)  
Antiguo 22/09/2006, 02:04
 
Fecha de Ingreso: diciembre-2005
Ubicación: Redondela (Galicia)
Mensajes: 368
Antigüedad: 18 años, 4 meses
Puntos: 1
Parsear XML con PHP

Pues eso, me gustaría que me comentáseis alguna buena forma de parsear un archivo XML para poder utilizarlo en PHP. He utilizado el buscador de este foro pero me omite la palabra XML por ser muy corta y no hay forma... Tambien, buscando en Google, he encontrado varias páginas con alguna que otra forma de hacerlo, pero casi todas me piden alguna extensión de PHP... y en un servidor compartido hay que amoldarse a lo que te dan, así que no me sirvieron

¿Alguna sugerencia? ¿Algún tutorial (preferiblemente en español)?

Gracias por vuestro tiempo
  #2 (permalink)  
Antiguo 22/09/2006, 03:50
 
Fecha de Ingreso: marzo-2005
Mensajes: 197
Antigüedad: 19 años, 2 meses
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;
  }
}
?>
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:04.