SimpleXML: Acceder a atributos
Veamos cómo acceder a los atributos de un documento XML similar a:
<?xml version="1.0" encoding="UTF-8"?> <feed> ... <link rel="alternate" href="http://www.domain.com"/> <image size="small">http://www.domain.com/images/44.jpg</image> <image size="large">http://www.domain.com/images/55.jpg</image> ... </feed>
Por ejemplo, para obtener el atributo href de link el código será:
<?php $feed = "http://www.domain.com/feed/info.xml"; // volcar el feed en un objeto SimpleXML $xml = simplexml_load_file($feed); echo $xml->link->attributes()->href; ?>
Para obtener los campos image utilizando el atributo size para filtrar, el código será:
<?php $feed = "http://www.domain.com/feed/info.xml"; // volcar el feed en un objeto SimpleXML $xml = simplexml_load_file($feed); foreach ($xml->image as $image) { switch((string) $image['size']) { case 'small': echo $image; break; case 'large': echo $image; break; } } ?>
Deja un comentario
