Comunidad de diseño web y desarrollo en internet online

XML en PHP

Citar            
MensajeEscrito el 24 Abr 2008 05:57 pm
hola a todos.. tengo un archivo php donde carga un xml pero quiero saber com acceder a las etiquetas del xml
este es el codigo de mi PHP

Código :

<?php

$Archivo = '../config.xml';

if (file_exists($Archivo)) {
    $XML = simplexml_load_file($Archivo); 
    foreach($XML as $Valor){
      echo $Valor -> title;
   }
} 
else {
    echo 'Error';
}
?>


y yo quisiera tomar estos valores del este xml
LINEA 12: <yweather:wind chill="28" direction="320" speed="4.83" />

LINEA 40: <yweather:forecast day="Thu" date="24 Apr 2008" low="14" high="31" text="Sunny" code="32" />


Código :

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>

<title>Yahoo! Weather - Bell Ville, AR</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Bell_Ville__AR/*http://weather.yahoo.com/forecast/ARCA2550_c.html</link>
<description>Yahoo! Weather for Bell Ville, AR</description>
<language>en-us</language>
<lastBuildDate>Thu, 24 Apr 2008 12:00 pm ART</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Bell Ville" region=""   country="AR"/>
<yweather:units temperature="C" distance="km" pressure="mb" speed="kph"/>
<yweather:wind chill="28"   direction="320"   speed="4.83" />
<yweather:atmosphere humidity="50"  visibility="15"  pressure="1010"  rising="0" />
<yweather:astronomy sunrise="7:37 am"   sunset="6:36 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Bell Ville, AR at 12:00 pm ART</title>
<geo:lat>-32.62</geo:lat>
<geo:long>-62.7</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Bell_Ville__AR/*http://weather.yahoo.com/forecast/ARCA2550_c.html</link>
<pubDate>Thu, 24 Apr 2008 12:00 pm ART</pubDate>
<yweather:condition  text="Haze"  code="21"  temp="28"  date="Thu, 24 Apr 2008 12:00 pm ART" />
<description><![CDATA[
<img src="http://l.yimg.com/us.yimg.com/i/us/we/52/21.gif"/><br />
<b>Current Conditions:</b><br />
Haze, 28 C<BR />
<BR /><b>Forecast:</b><BR />
Thu - Sunny. High: 31 Low: 14<br />
Fri - Sunny. High: 31 Low: 12<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Bell_Ville__AR/*http://weather.yahoo.com/forecast/ARCA2550_c.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]></description>
<yweather:forecast day="Thu" date="24 Apr 2008" low="14" high="31" text="Sunny" code="32" />
<yweather:forecast day="Fri" date="25 Apr 2008" low="12" high="31" text="Sunny" code="32" />
<guid isPermaLink="false">ARCA2550_2008_04_24_12_00_ART</guid>
</item>
</channel>
</rss><!-- api3.weather.sp1.yahoo.com compressed/chunked Thu Apr 24 10:23:50 PDT 2008 -->

Por Zis

Claber

314 de clabLevel

1 tutorial

Genero:Masculino  

Bell Ville - Cordoba - Argentina

firefox
Citar            
MensajeEscrito el 25 Abr 2008 12:40 am
Para poder accesar a los atributos que buscan basta con que hagas esto:

Código :

$chill = $Valor['chill'];


En otras palabras los accesas como si fuera un arreglo.

Por Odin

Claber

639 de clabLevel

2 tutoriales

Genero:Masculino   Premio_Secretos

El valle de las hamacas

firefox
Citar            
MensajeEscrito el 25 Abr 2008 12:58 am
probé hacerlo de esa forma pero no funciona :crap:

me podrían explicar mas detalladamente

gracias a todos

Por Zis

Claber

314 de clabLevel

1 tutorial

Genero:Masculino  

Bell Ville - Cordoba - Argentina

firefox
Citar            
MensajeEscrito el 25 Abr 2008 02:02 am
Si dices que lo has probado de esa manera podrías postear el código para ver si no hay ningún error.

Por Odin

Claber

639 de clabLevel

2 tutoriales

Genero:Masculino   Premio_Secretos

El valle de las hamacas

firefox
Citar            
MensajeEscrito el 25 Abr 2008 02:33 am
no no me refiero a que probé hacerlo de la forma que me dices no modifique nada solo puse la variable que me dijiste tu pero de todas formas mucho no entendí como había que hacerlo

Por Zis

Claber

314 de clabLevel

1 tutorial

Genero:Masculino  

Bell Ville - Cordoba - Argentina

firefox
Citar            
MensajeEscrito el 25 Abr 2008 05:20 am
Ok, trataré de explicarlo más despacio.

Al accesar a los datos de un archivo XML de la manera que tu lo estas haciendo, puedes accesar a los nodos hijos utilizando el operador flecha ( -> )o a los atributos por medio de los corchetes (como si fuera un arreglo).

Código :

$nodoXML->hijo; //accedes al nodo hijo
$nodoXML['atributo']; //accedes al valor del atributo


Viendo esto en un archivo XML se puede decir que estan estructurados así:

Código :

<nodoXML atributo="valor" >
     <hijo atributo="valor" />
</nodoXML>


La razón por la que el código que poste antes no te fucionó es porque no lo puse para resolver tu problema sino para explicarte la manera en la que se soluciona. Acá en cristalab podemos ayudarte a resolver tus problemas, pero no los resolveremos por tí U_U

PD. Este código tampoco funcionará si solo lo copias y pegas.

Por Odin

Claber

639 de clabLevel

2 tutoriales

Genero:Masculino   Premio_Secretos

El valle de las hamacas

firefox
Citar            
MensajeEscrito el 29 Abr 2008 02:29 am
amigo, no estoy pidiendo que me lo resulvas ni tampoco copie y pegue el codigo.. pero es como si faltara algo... medices que para cagar los atributos de un nodo se escribe de la siguiente forma

Código :

$nodoXML[\'atributo\']; //accedes al valor del atributo


bueno pero yo intente ingresar a los atributos del nodo de esta forma:


Código :


$Nodo = $nodoXML->yweather;
$Nodo[\'chill\'];

<yweather:location city=\"Bell Ville\" region=\"\"   country=\"AR\"/>

<yweather:units temperature=\"C\" distance=\"km\" pressure=\"mb\" speed=\"kph\"/>

<yweather:wind chill=\"28\"   direction=\"320\"   speed=\"4.83\" />

<yweather:atmosphere humidity=\"50\"  visibility=\"15\"  pressure=\"1010\"  rising=\"0\" />

<yweather:astronomy sunrise=\"7:37 am\"   sunset=\"6:36 pm\"/>


es decir se como se accede al nodo y a la propiedad pero en este caso el nodo esta definido vaias veces y tiene muchos atributor por lo que estoy desorientado y me urge saber como solucionarlo, no pido que me lo escriban pero solo que me orienten un poco mejor para de una ves por todas poder terminar :cry:

un saludo y gracias

Por Zis

Claber

314 de clabLevel

1 tutorial

Genero:Masculino  

Bell Ville - Cordoba - Argentina

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.