Foros del Web » Programando para Internet » PHP »

¿Como leer atributos con Simplexml?

Estas en el tema de ¿Como leer atributos con Simplexml? en el foro de PHP en Foros del Web. xml : Código: <media:group> <media:category label="Viajes y eventos" scheme="http: //gdata.youtube. com/schemas/2007/categories.cat">Travel</media:category> <media:content url="http: //ww w.youtube. com/v/iO7zACjAQTY&f=videos&app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="238" yt:format="5"/> <media:content url="rtsp: //rtsp2.youtube. ...
  #1 (permalink)  
Antiguo 20/08/2009, 04:14
 
Fecha de Ingreso: enero-2008
Mensajes: 43
Antigüedad: 16 años, 3 meses
Puntos: 0
¿Como leer atributos con Simplexml?

xml:
Código:
<media:group>
<media:category label="Viajes y eventos" scheme="http: //gdata.youtube. com/schemas/2007/categories.cat">Travel</media:category>
<media:content url="http: //ww w.youtube. com/v/iO7zACjAQTY&f=videos&app=youtube_gdata" type="application/x-shockwave-flash" medium="video" isDefault="true" expression="full" duration="238" yt:format="5"/>
<media:content url="rtsp: //rtsp2.youtube. com/CiILENy73wIaGQk2QcAoAPPuiBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="238" yt:format="1"/>
<media:content url="rtsp: //rtsp2.youtube. com/CiILENy73wIaGQk2QcAoAPPuiBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" type="video/3gpp" medium="video" expression="full" duration="238" yt:format="6"/>
<media:description type="plain">http: //ww w.jtorres. com.ar/blog</media:description>
−
<media:keywords>
Garganta, del, Diablo, Parque, nacional, Iguazu, cataratas, Argentina
</media:keywords>
<media:player url="http: //ww w.youtube. com/watch?v=iO7zACjAQTY"/>
<media:thumbnail url="http: //i.ytimg. com/vi/iO7zACjAQTY/2.jpg" height="90" width="120" time="00:01:59"/>
<media:thumbnail url="http: //i.ytimg. com/vi/iO7zACjAQTY/1.jpg" height="90" width="120" time="00:00:59.500"/>
<media:thumbnail url="http: //i.ytimg. com/vi/iO7zACjAQTY/3.jpg" height="90" width="120" time="00:02:58.500"/>
<media:thumbnail url="http: //i.ytimg. com/vi/iO7zACjAQTY/0.jpg" height="240" width="320" time="00:01:59"/>
−
<media:title type="plain">
Garganta del Diablo -  Parque nacional Iguazu - cataratas Argentina
</media:title>
Estoy desvelado de tratar de leer el link del atributo de <media:trumbnail> llamado url

¿Alguien sabe como se lee?
  #2 (permalink)  
Antiguo 20/08/2009, 17:28
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: ¿Como leer atributos con Simplexml?

Verificaste la parte que dice simpleXMLElement::attributes. Tienen un ejemplo claro de como sacar los atributos.
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #3 (permalink)  
Antiguo 21/08/2009, 05:46
 
Fecha de Ingreso: enero-2008
Mensajes: 43
Antigüedad: 16 años, 3 meses
Puntos: 0
Respuesta: ¿Como leer atributos con Simplexml?

Gracias, creo que el problema es que no se como leer donde la etiqueta esta dividida con ":"(dos puntos)

Código PHP:
    foreach($results->group->thumbnail[0]->attributes() as $a => $b) {
    echo 
$a,'="',$b,"\"\n";
    } 
  #4 (permalink)  
Antiguo 21/08/2009, 07:28
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: ¿Como leer atributos con Simplexml?

¿Pero tu quieres la parte que dice thumbnail o la parte que dice el url?
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #5 (permalink)  
Antiguo 21/08/2009, 09:00
 
Fecha de Ingreso: enero-2008
Mensajes: 43
Antigüedad: 16 años, 3 meses
Puntos: 0
Respuesta: ¿Como leer atributos con Simplexml?

esto es lo que me interesa
"http: //i.ytimg. com/vi/iO7zACjAQTY/2.jpg"

Lo podria sacar con preg_match_all pero prefiero hacerlo de la forma correcta.
  #6 (permalink)  
Antiguo 21/08/2009, 09:02
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: ¿Como leer atributos con Simplexml?

Si te fijas en el foreach que tomaste de ejemplo se asignan dos variables $a y $b. $b es la que tiene lo que estas pidiendo
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos
  #7 (permalink)  
Antiguo 21/08/2009, 09:07
 
Fecha de Ingreso: enero-2008
Mensajes: 43
Antigüedad: 16 años, 3 meses
Puntos: 0
Respuesta: ¿Como leer atributos con Simplexml?

si eso recupera la informacion, es que no estoy seguro como armar la ruta nunca use estas etiquetas asi
Código:
<media:group>
    <media:thumbnail url="dato"/>
</media:title>
  #8 (permalink)  
Antiguo 21/08/2009, 11:52
Avatar de abimaelrc
Colaborador
 
Fecha de Ingreso: mayo-2009
Ubicación: En el planeta de Puerto Rico
Mensajes: 14.734
Antigüedad: 15 años
Puntos: 1517
Respuesta: ¿Como leer atributos con Simplexml?

Buscando y buscando encontre esto que te puede ayudar a saber como sacar la informacion. Una leve modificación para que puedas entender como hacerlo. Lee sobre este post para que entiendas que tienes que hacer.

http://www.forosdelweb.com/f68/duda-simplexml-559577/


Código PHP:
Ver original
  1. <?php
  2. $xml = "<?xml version='1.0'?>
  3. <root>
  4.     <ns:book xmlns:ns=\"http://quickstart.samples/xsd\">
  5.         <ns:title url=\"http://i.ytimg.com/vi/iO7zACjAQTY/2.jpg\">Titulo</ns:title>
  6.     </ns:book>
  7.     <title>titlea</title>
  8. </root>";
  9.  
  10. $xml = simplexml_load_string($xml);
  11. $ns = $xml->getNamespaces(true);
  12. $xml->registerXPathNamespace('c', $ns['ns']);
  13. $attribute = $xml->xpath("//c:title");
  14. print_r($attribute);
  15. ?>


Como sacarlo

Código PHP:
Ver original
  1. <?php
  2. $xml = "<?xml version='1.0'?>
  3. <root>
  4.     <ns:book xmlns:ns=\"http://quickstart.samples/xsd\">
  5.         <ns:title url=\"http://i.ytimg.com/vi/iO7zACjAQTY/2.jpg\">Titulo</ns:title>
  6.     </ns:book>
  7.     <title>titlea</title>
  8. </root>";
  9.  
  10. $xml = simplexml_load_string($xml);
  11. $ns = $xml->getNamespaces(true);
  12. $xml->registerXPathNamespace('c', $ns['ns']);
  13. $attribute = $xml->xpath("//c:title");
  14. echo $attribute[0]->attributes();
  15. ?>
__________________
Verifica antes de preguntar.
Los verdaderos amigos se hieren con la verdad, para no perderlos con la mentira. - Eugenio Maria de Hostos

Última edición por abimaelrc; 21/08/2009 a las 11:54 Razón: Añadi el segundo codigo
  #9 (permalink)  
Antiguo 21/08/2009, 21:12
 
Fecha de Ingreso: enero-2008
Mensajes: 43
Antigüedad: 16 años, 3 meses
Puntos: 0
Respuesta: ¿Como leer atributos con Simplexml?

Gracias!, Me salvaste de que me explotara el cerebro.
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 15:53.