Foros del Web » Programando para Internet » PHP »

Parser XML

Estas en el tema de Parser XML en el foro de PHP en Foros del Web. Veran he leido el articulo de zonaphp.com y he tomado su codigo pero al tratar de modificarlo no me sirve Aqui el codigo: Código PHP: ...
  #1 (permalink)  
Antiguo 27/02/2005, 02:05
 
Fecha de Ingreso: enero-2004
Mensajes: 431
Antigüedad: 20 años, 3 meses
Puntos: 6
Parser XML

Veran he leido el articulo de zonaphp.com y he tomado su codigo pero al tratar de modificarlo no me sirve
Aqui el codigo:
Código PHP:
<? 
  
// parser de rss 
   
  // obtenemos el rss desde el sitio de zonaphp :) 
  
if(!($fp fopen("http://steamid.com/ircbot.php?qvalue=0:0:205893""r"))) 
  { 
   die(
"No puedo abrir el archivo!"); 
  } 
   
  
$rss ""
  while(!
feof($fp)) 
  { 
   
$rss .= fread($fp4096); 
  } 
  
fclose($fp); 
   
  
// inicializamos los auxiliares 
  
$contenido ""
  
$tag_actual ""
  
$en_item false
  
$item 0

  
// inicializamos arrays 
  
$items = Array(); 
   
  
// creamos el parser 
  
$parser xml_parser_create(); 
   
  
// definimos los "handlers" 
  
xml_set_element_handler($parser"tag_abre""tag_cierra"); 
  
xml_set_character_data_handler($parser"tag_contenido"); 
   
  
// declaramos las funciones 
  
function tag_abre($parser$nombre$atributos
  { 
   global 
$tag_actual$en_item

   
$tag_actual $nombre

   switch(
$nombre
   { 
    case 
"RESULT"
      
$en_item true
     break; 
   } 
  } 

  function 
tag_contenido($parser$valor
  { 
   global 
$contenido

   
$contenido .= $valor
  } 

  function 
tag_cierra($parser$nombre
  { 
   global 
$tag_actual$contenido$item$en_item,$items

   
$contenido trim($contenido); 

   switch(
$nombre
   { 
    case 
"PLAYER_STEAMID"
        if(
$en_item) {
       
$items[$item]["PLAYER_STEAMID"] = $contenido;
    }
     break; 
    case 
"TEAM_LEAGUE"
                if(
$en_item) {

        
$items[$item]["TEAM_LEAGUE"] = $contenido
    }
     break; 
    case 
"TEAM_NAME"
                if(
$en_item) {

       
$items[$item]["TEAM_NAME"] = $contenido
    }
     break; 
         case 
"TEAM_IRC"
                     if(
$en_item) {

       
$items[$item]["TEAM_IRC"] = $contenido
         }
     break; 
 case 
"PLAYER_NAME"
             if(
$en_item) {

       
$items[$item]["PLAYER_NAME"] = $contenido
 }
     break; 
   } 

   
$tag_actual ""
   
$contenido ""
  } 
   
  
// procedemos a procesar el archivo, en caso de error 
  // lo indicamos y terminamos 
  
if(!xml_parse($parser$rsstrue)) 
  { 
   die(
"Error en la linea "
       
xml_get_current_line_number($parser). 
       
": ".xml_error_string(xml_get_error_code($parser))); 
  } 
   
  
// destruimos el parser 
  
xml_parser_free($parser); 

  print 
$item["PLAYER_STEAMID"];
  print 
$item["TEAM_LEAGUE"];
  print 
$item["TEAM_NAME"];
  print 
$item["TEAM_IRC"];
  print 
$item["PLAYER_NAME"];
?>
Al ejecutarlo me muestra la pantalla en blanko
Alguna idea?

__________________
El que teme preguntar, se averguenza de aprender.
  #2 (permalink)  
Antiguo 27/02/2005, 13:19
 
Fecha de Ingreso: diciembre-2002
Mensajes: 100
Antigüedad: 21 años, 4 meses
Puntos: 1
Puedes usar este script para leer un xml, lo que hace es entregarte un Array como resultado.

rss.php

Código PHP:
<?php
function GetXMLTree ($xmldata)
{
    
// we want to know if an error occurs
    
ini_set ('track_errors''1');

    
$xmlreaderror false;

    
$parser xml_parser_create ('ISO-8859-1');
    
xml_parser_set_option ($parserXML_OPTION_SKIP_WHITE1);
    
xml_parser_set_option ($parserXML_OPTION_CASE_FOLDING0);
    if (!
xml_parse_into_struct ($parser$xmldata$vals$index)) {
        
$xmlreaderror true;
        echo 
"error";
    }
    
xml_parser_free ($parser);

    if (!
$xmlreaderror) {
        
$result = array ();
        
$i 0;
        if (isset (
$vals [$i]['attributes']))
            foreach (
array_keys ($vals [$i]['attributes']) as $attkey)
            
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];

        
$result [$vals [$i]['tag']] = array_merge ($attributesGetChildren ($vals$i'open'));
    }

    
ini_set ('track_errors''0');
    return 
$result;
}

function 
GetChildren ($vals, &$i$type)
{
    if (
$type == 'complete') {
        if (isset (
$vals [$i]['value']))
            return (
$vals [$i]['value']);
        else
            return 
'';
    }

    
$children = array (); // Contains node data

    /* Loop through children */
    
while ($vals [++$i]['type'] != 'close') {
        
$type $vals [$i]['type'];
        
// first check if we already have one and need to create an array
        
if (isset ($children [$vals [$i]['tag']])) {
            if (
is_array ($children [$vals [$i]['tag']])) {
                
$temp array_keys ($children [$vals [$i]['tag']]);
                
// there is one of these things already and it is itself an array
                
if (is_string ($temp [0])) {
                    
$a $children [$vals [$i]['tag']];
                    unset (
$children [$vals [$i]['tag']]);
                    
$children [$vals [$i]['tag']][0] = $a;
                }
            } else {
                
$a $children [$vals [$i]['tag']];
                unset (
$children [$vals [$i]['tag']]);
                
$children [$vals [$i]['tag']][0] = $a;
            }

            
$children [$vals [$i]['tag']][] = GetChildren ($vals$i$type);
        } else
            
$children [$vals [$i]['tag']] = GetChildren ($vals$i$type);
        
// I don't think I need attributes but this is how I would do them:
        
if (isset ($vals [$i]['attributes'])) {
            
$attributes = array ();
            foreach (
array_keys ($vals [$i]['attributes']) as $attkey)
            
$attributes [$attkey] = $vals [$i]['attributes'][$attkey];
            
// now check: do we already have an array or a value?
            
if (isset ($children [$vals [$i]['tag']])) {
                
// case where there is an attribute but no value, a complete with an attribute in other words
                
if ($children [$vals [$i]['tag']] == '') {
                    unset (
$children [$vals [$i]['tag']]);
                    
$children [$vals [$i]['tag']] = $attributes;
                }
                
// case where there is an array of identical items with attributes
                
elseif (is_array ($children [$vals [$i]['tag']])) {
                    
$index count ($children [$vals [$i]['tag']]) - 1;
                    
// probably also have to check here whether the individual item is also an array or not or what... all a bit messy
                    
if ($children [$vals [$i]['tag']][$index] == '') {
                        unset (
$children [$vals [$i]['tag']][$index]);
                        
$children [$vals [$i]['tag']][$index] = $attributes;
                    }
                    
$children [$vals [$i]['tag']][$index] = array_merge ($children [$vals [$i]['tag']][$index], $attributes);
                } else {
                    
$value $children [$vals [$i]['tag']];
                    unset (
$children [$vals [$i]['tag']]);
                    
$children [$vals [$i]['tag']]['value'] = $value;
                    
$children [$vals [$i]['tag']] = array_merge ($children [$vals [$i]['tag']], $attributes);
                }
            } else
                
$children [$vals [$i]['tag']] = $attributes;
        }
    }

    return 
$children;
}

//Ejemplo, lectura de los ultimos post en Foros del WEB - ML

$url "http://www.forosdelweb.com/index.xml"//URL del XML a leer
$contents file_get_contents($url);
$data GetXMLTree ($contents);

//Resultado entregado en un Array
//print_r($data);


echo $data["rss"]["channel"]["description"]; //Descripción
echo "<br><br>";

//Cuantos item?
$t count($data["rss"]["channel"]["item"]);

//Recorremos el array
for($i 0$i $t$i++){
echo 
"<br><br>";
echo 
"<a href='" $data["rss"]["channel"]["item"][$i]["link"] . "' target='_blank'>" $data["rss"]["channel"]["item"][$i]["title"] . "</a>";
echo 
"<br>";
echo 
$data["rss"]["channel"]["item"][$i]["description"];
}


?>
  #3 (permalink)  
Antiguo 27/02/2005, 15:09
 
Fecha de Ingreso: enero-2004
Mensajes: 431
Antigüedad: 20 años, 3 meses
Puntos: 6
veras al parecer este si va a servir por que si pongo el print_r($data); me regresa esto:
Array ( [results] => Array ( [result] => Array ( [player_steamid] => 0:0:146207 [team_league] => CAL-P [team_name] => Cyberglobe (10-3) [team_irc] => #Cyberglobe [player_alias] => [player_name] => geoff rigsby ) ) )
Pero no entiendo esta parte:
Código PHP:
echo $data["rss"]["channel"]["description"]; //Descripción 
echo "<br><br>"

//Cuantos item? 
$t count($data["rss"]["channel"]["item"]); 

//Recorremos el array 
for($i 0$i $t$i++){ 
echo 
"<br><br>"
echo 
"<a href='" $data["rss"]["channel"]["item"][$i]["link"] . "' target='_blank'>" $data["rss"]["channel"]["item"][$i]["title"] . "</a>"
echo 
"<br>"
echo 
$data["rss"]["channel"]["item"][$i]["description"]; 

Supongo ke sera por ke no se nada de XML, talvez me puedas dar una ayudadita lo que busko es parsear esta info:
http://steamid.com/ircbot.php?qvalue=0:0:146207

  #4 (permalink)  
Antiguo 27/02/2005, 17:11
 
Fecha de Ingreso: enero-2004
Mensajes: 431
Antigüedad: 20 años, 3 meses
Puntos: 6
ya he solucionado el problema GRACIAS

__________________
El que teme preguntar, se averguenza de aprender.
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:43.