Foros del Web » Programando para Internet » PHP »

attributes img CDATA xml php

Estas en el tema de attributes img CDATA xml php en el foro de PHP en Foros del Web. buenas a todos tengo una problemilla para extraer solo attr img desde un archivo xml aqui os pongo el ej: @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código XML: Ver ...
  #1 (permalink)  
Antiguo 23/01/2012, 16:59
 
Fecha de Ingreso: noviembre-2009
Mensajes: 535
Antigüedad: 14 años, 5 meses
Puntos: 25
attributes img CDATA xml php

buenas a todos
tengo una problemilla para extraer solo attr img desde un archivo xml
aqui os pongo el ej:
Código XML:
Ver original
  1. <description>
  2. <![CDATA[
  3. <img src="http://ruta_img.gif"/><br />
  4. ]]>
  5. </description>
alguna idea...
__________________
cada vez que aprendes algo te crees que no sabes nada
  #2 (permalink)  
Antiguo 23/01/2012, 18:19
Avatar de andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 9 meses
Puntos: 793
Respuesta: attributes img CDATA xml php

Usa DOM para obtener el atributo src o para sacar el código html.

Muestra que llevas.

http://www.php.net/manual/en/class.domdocument.php
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP
  #3 (permalink)  
Antiguo 24/01/2012, 06:16
 
Fecha de Ingreso: noviembre-2009
Mensajes: 535
Antigüedad: 14 años, 5 meses
Puntos: 25
Respuesta: attributes img CDATA xml php

en primero lugar agradezco tu respuesta aun lo estoy mirando...
aqui pongo todo mi ejemplo:
lo que quiero es mostrar el tiempo de yahoo.waeter en mi pagina la respuesta es de formato xml en esto no tengo ninguna problema, pero lo que quiero es mostrar solo la imagen del tiempo y la temperatura sin logo de yahoo ni otra cosa
codigo xml
Código XML:
Ver original
  1. <rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
  2. <channel>
  3. <title>Yahoo! Weather - Sunnyvale, CA</title>
  4. <link>...</link>
  5. <description>Yahoo! Weather for Sunnyvale, CA</description>
  6. <language>en-us</language>
  7. <lastBuildDate>Mon, 23 Jan 2012 10:02 am PST</lastBuildDate>
  8. <ttl>60</ttl>
  9. <yweather:location city="Sunnyvale" region="CA" country="United States"/>
  10. <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
  11. <yweather:wind chill="54" direction="260" speed="9"/>
  12. <yweather:atmosphere humidity="82" visibility="10" pressure="29.88" rising="0"/>
  13. <yweather:astronomy sunrise="7:18 am" sunset="5:21 pm"/>
  14. <image>...</image>
  15. <item>
  16. <title>Conditions for Sunnyvale, CA at 10:02 am PST</title>
  17. <geo:lat>37.37</geo:lat>
  18. <geo:long>-122.04</geo:long>
  19. <link>...</link>
  20. <pubDate>Mon, 23 Jan 2012 10:02 am PST</pubDate>
  21. <yweather:condition text="Cloudy" code="26" temp="54" date="Mon, 23 Jan 2012 10:02 am PST"/>
  22. <description>
  23. <![CDATA[
  24. <img src="http://l.yimg.com/a/i/us/we/52/26.gif"/><br /> <b>Current Conditions:</b><br /> Cloudy, 54 F<BR /> <BR /><b>Forecast:</b><BR /> Mon - AM Rain. High: 58 Low: 44<br /> Tue - Partly Cloudy. High: 63 Low: 45<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA1116_f.html">Full Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
  25. ]]>
  26. </description>
  27. <yweather:forecast day="Mon" date="23 Jan 2012" low="44" high="58" text="AM Rain" code="12"/>
  28. <yweather:forecast day="Tue" date="24 Jan 2012" low="45" high="63" text="Partly Cloudy" code="30"/>
  29. <guid isPermaLink="false">USCA1116_2012_01_24_7_00_PST</guid>
  30. </item>
  31. </channel>
  32. </rss>
codigo php

Código PHP:
Ver original
  1. $xml_file = 'http://weather.yahooapis.com/forecastrss?w=2502265';
  2. // The XML data file with whitespace such as tabs
  3. // Load xml data.
  4. $xml_f = simplexml_load_string($xml_file);
  5. $x = $xml_file->channel->item->description;
  6. $xml = file_get_contents($xml_file);
  7. // Strip whitespace between xml tags
  8. $xml = preg_replace('~\s*(<([^>]*)>[^<]*</\2>|<[^>]*>)\s*~','$1',$xml);
  9. // Convert CDATA into xml nodes.
  10. $result = simplexml_load_string($xml,$x, LIBXML_NOCDATA);
  11. // Return JSON.
  12. //var_dump(json_encode($xml));
  13. foreach($result->channel->item->description as $e){
  14.     echo $e;
y desta forma consigo mostrar el CDATA pero en formato html y lo que quiero solo attr de img no mas cosas
gracias
__________________
cada vez que aprendes algo te crees que no sabes nada
  #4 (permalink)  
Antiguo 24/01/2012, 12:30
Avatar de andresdzphp
Colaborador
 
Fecha de Ingreso: julio-2011
Ubicación: $this->Colombia;
Mensajes: 2.749
Antigüedad: 12 años, 9 meses
Puntos: 793
Respuesta: attributes img CDATA xml php

Algo así mas o menos, la imagen con el valor de la temperatura:

Código PHP:
Ver original
  1. <?php
  2. $doc = new DOMDocument();
  3. $xml = file_get_contents('http://weather.yahooapis.com/forecastrss?w=2502265');
  4. $doc->loadXML($xml);
  5. $des = $doc->getElementsByTagName('description');
  6.  
  7. $con = $doc->getElementsByTagNameNS($doc->lookupNamespaceUri('yweather'), 'condition');
  8. $temp = $con->item(0)->attributes->getNamedItem('temp')->nodeValue;
  9.  
  10. $doc->loadHTML($des->item(1)->nodeValue);
  11. $imgs = $doc->getElementsByTagName('img');
  12. $img = $imgs->item(0)->attributes->getNamedItem('src')->nodeValue;
  13. ?>
  14. <img src="<?php echo $img; ?>" /><br />
  15. <?php echo $temp . ' F'; ?>
__________________
Si sabemos como leer e interpretar el manual será mucho más fácil aprender PHP. En lugar de confiar en ejemplos o copiar y pegar - PHP
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

SíEste tema le ha gustado a 1 personas




La zona horaria es GMT -6. Ahora son las 14:57.