Foros del Web » Programando para Internet » PHP »

parsear xml...

Estas en el tema de parsear xml... en el foro de PHP en Foros del Web. buenas noches... soy nuevo en php.. y no entiendo muxo.... si me pueden ayudar... necesito el pronostico del tiempo de este link... http://clima.msn.com/RSS.aspx?wealocations=wc:PAXX0005&weadegreetype=C& culture=es-US y ...
  #1 (permalink)  
Antiguo 06/07/2008, 17:25
Avatar de engranaje  
Fecha de Ingreso: febrero-2008
Mensajes: 45
Antigüedad: 16 años, 1 mes
Puntos: 1
Pregunta parsear xml...

buenas noches... soy nuevo en php.. y no entiendo muxo.... si me pueden ayudar...
necesito el pronostico del tiempo de este link...
http://clima.msn.com/RSS.aspx?wealocations=wc:PAXX0005&weadegreetype=C& culture=es-US
y me dijeron ke puedo parsearlo con esta funcion en php...
necesito la condicion actual.. el dibujito del tiempo, la temperatura actual.. y el pronosctico para cinco dias...
alguien me puede explicar mas o menos como funciona... gracias..


Código PHP:
//**************************************
    //     
    // Name: MEL :: RSS And XML Parse Functi
    //     on
    // Description:**UPDATED** This function
    //     will parse your favorite XML and RSS new
    //     s feeds in a nice format. Only one varia
    //     ble to set up and easy to integrate and 
    //     modify. Tested on several sources. Let m
    //     e know this has been useful. Comments an
    //     d suggestions are very much appreciate i
    //     t. **Please Vote**
    // By: Melvin D. Nava
    //
    //This code is copyrighted and has    // limited warranties.Please see http://
    //     www.Planet-Source-Code.com/vb/scripts/Sh
    //     owCode.asp?txtCodeId=1320&lngWId=8    //for details.    //**************************************
    //     
    
    <?php
    
//
    // The Function
    // By Melvin D. Nava
    // http://mdnava.network.com.ve/
    //
    
function parse_rss($f) {
        
$xmlfile fopen($f'r');
        if (!
$xmlfile) die('cannot open the xml file');
        
$readfile fread($xmlfile ,40000);
        
$parsefile eregi("<item>(.*)</item>"$readfile ,$arrayreg);
        
$filechunks explode("<item>"$arrayreg[0]);
        
$count count($filechunks);
        echo 
'<font face=verdana><ul>';
        for(
$i=$i<=$count-;$i++) {
            
ereg("<title>(.*)</title>",$filechunks[$i], $title);
            
ereg("<link>(.*)</link>",$filechunks[$i], $links);
            
ereg("<description>(.*)</description>",$filechunks[$i], $description);
            echo 
str_replace('hxaxh','a',"<li><font style='font-size: 12px;'><hxaxh target=_blank href ='$links[1]'\>".utf8_decode($title[1])."</hxaxh></font>");
            echo 
"<br><font color=gray style='font-size: 10px;'>".utf8_decode($description[1])."</font></li>";
        }
        
// feel free to remove next notice
        // is not needed by this function
        
echo '</ul><font style="font-size: 10px;">Better News Grabber by ';
        echo 
str_replace('hxaxh','a','<hxaxh target=_blank href="http://mdnava.network.com.ve/">Melvin D. Nava</hxaxh></font></font></font>');
    }
    
//
    // The Example
    //
    
echo '<h1>Star Wars News</h1>';
    
$xmlfeed 'http://www.starwars.com/community/webmasters/starwars.rdf';
    
parse_rss($xmlfeed);
    
?>
  #2 (permalink)  
Antiguo 07/07/2008, 18:53
Avatar de WyLY  
Fecha de Ingreso: enero-2002
Ubicación: Cancun, México.
Mensajes: 506
Antigüedad: 22 años, 3 meses
Puntos: 6
Respuesta: parsear xml...

Hola amigo, yo no sé mucho de xml, pero sé algo de php y ya que nadie se ha animado a responderte xD, trataré de ayudarte:
Código PHP:
    <?php
//Crea una funcion (con esto realizas una acción digamos encapsulada para luego darle uso más adelante.
    
function parse_rss($f) {
//con fopen abres un archivo, en este caso abre "read" la variable $f que supongo es el archivo.
        
$xmlfile fopen($f'r');
//Crea un if por si no puede.
        
if (!$xmlfile) die('cannot open the xml file');
//Lee el archivo abierto.
        
$readfile fread($xmlfile ,40000);
/
Busca coincidencias en lo que leeen este casi a partir de <item>(.*)</item>, más infohttp://www.php.net/eregi
        
$parsefile eregi("<item>(.*)</item>"$readfile ,$arrayreg);
//Hacer un explode para divir el cotenido y utilizarlo.
        
$filechunks explode("<item>"$arrayreg[0]);
//cuenta las divisiones
        
$count count($filechunks);
//imprimire, en este caso el el tipo de fuente para un futuro bucle.
        
echo '<font face=verdana><ul>';
//Inicia el bucle por el cual imprimira lo leeido y divido anteriormentee
        
for($i=$i<=$count-;$i++) {
            
ereg("<title>(.*)</title>",$filechunks[$i], $title);
            
ereg("<link>(.*)</link>",$filechunks[$i], $links);
            
ereg("<description>(.*)</description>",$filechunks[$i], $description);
            echo 
str_replace('hxaxh','a',"<li><font style='font-size: 12px;'><hxaxh target=_blank href ='$links[1]'\>".utf8_decode($title[1])."</hxaxh></font>");
            echo 
"<br><font color=gray style='font-size: 10px;'>".utf8_decode($description[1])."</font></li>";
        }
//Termina bucle e imprime otras cositas..
        // feel free to remove next notice
        // is not needed by this function
        
echo '</ul><font style="font-size: 10px;">Better News Grabber by ';
        echo 
str_replace('hxaxh','a','<hxaxh target=_blank href="http://mdnava.network.com.ve/">Melvin D. Nava</hxaxh></font></font></font>');
    }
    
//
    // The Example
    //
    
echo '<h1>Star Wars News</h1>';
    
$xmlfeed 'http://www.starwars.com/community/webmasters/starwars.rdf';
    
parse_rss($xmlfeed);
    
?>
Bueno, espero te ayude algo, algunas sentencias utilizadas:
fopen, fread, echo, explode, eregi o ereg, for(bucles while and if, etc), MAS INFO,

http://www.php.net/LASENTENCIA QUE BUSCAS.

un abrazo.
__________________
I do techy stuff for a living and practice MMA with my dog... He always wins.
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 14:50.