johnslater Posted December 16, 2009 Share Posted December 16, 2009 I have a problem while parsing xml in PHP using the PHP function "simplexml" THe problem i'm facing i believe is mainly down to the xml but i wondered if there is a solution anyway. In my XML i have these lines... <image size="small">http://userserve-ak.last.fm/serve/34/25607637.jpg</image> <image size="medium">http://userserve-ak.last.fm/serve/64/25607637.jpg</image> <image size="large">http://userserve-ak.last.fm/serve/126/25607637.jpg</image> <image size="extralarge">http://userserve-ak.last.fm/serve/252/25607637.jpg</image> ... i want to be able to select a specific image from the size attribute. Here is what i'm using to grab it in PHP... $xml->artist->image['size']; but this only grabs the value of the "size" attribute, i want the value between the tags. Thanks in advanced. Link to comment https://forums.phpfreaks.com/topic/185367-php-simplexml-help-with-attributes/ Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 post more code on how you are processing it Link to comment https://forums.phpfreaks.com/topic/185367-php-simplexml-help-with-attributes/#findComment-978564 Share on other sites More sharing options...
johnslater Posted December 16, 2009 Author Share Posted December 16, 2009 <? $xml = simplexml_load_file('http://ws.audioscrobbler.com/2.0/user/slaterjohn/topartists.xml?period=12month'); $array = $xml->artist; foreach ($array as $value) { $name = $value->name; $playcount = $value->playcount; $url = $value->url; $image = $value->image; $i++; echo $i.'.) '; echo '<a href="'.$url.'">'.$name.'</a>'; echo ' - <strong>'.$playcount.'</strong>'; echo $image; echo '<br />'; } ?> this is what i have so far. Link to comment https://forums.phpfreaks.com/topic/185367-php-simplexml-help-with-attributes/#findComment-978567 Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 your code seems to work fine ? what change do you want to make to it, please elaborate Link to comment https://forums.phpfreaks.com/topic/185367-php-simplexml-help-with-attributes/#findComment-978575 Share on other sites More sharing options...
johnslater Posted December 16, 2009 Author Share Posted December 16, 2009 <image size="small">http://userserve-ak.last.fm/serve/34s/8634917.jpg</image> <image size="medium">http://userserve-ak.last.fm/serve/64s/8634917.jpg</image> <image size="large">http://userserve-ak.last.fm/serve/126/8634917.jpg</image> − <image size="extralarge"> http://userserve-ak.last.fm/serve/300x300/8634917.jpg </image> The XML only pulls the small image (the first one) and i want to be able to select an image size, for example "extralarge" but i cannot select an image by size. Link to comment https://forums.phpfreaks.com/topic/185367-php-simplexml-help-with-attributes/#findComment-978578 Share on other sites More sharing options...
rajivgonsalves Posted December 16, 2009 Share Posted December 16, 2009 something like this should work <?php $xml = simplexml_load_file('http://ws.audioscrobbler.com/2.0/user/slaterjohn/topartists.xml?period=12month'); $array = $xml->artist; for ($i=0; $i< count($xml->artist); $i++) { echo getImage($xml->artist[$i]->image, 'medium'); } function getImage(&$node, $size) { for($i=0; $i<count($node);$i++) { if ($node[$i]->attributes()->size[0] == $size) { return $node[$i]; } } } ?> Link to comment https://forums.phpfreaks.com/topic/185367-php-simplexml-help-with-attributes/#findComment-978588 Share on other sites More sharing options...
salathe Posted December 16, 2009 Share Posted December 16, 2009 Or, use XPath to query only for the images that you want, like: $xml = simplexml_load_file('http://ws.audioscrobbler.com/2.0/user/slaterjohn/topartists.xml?period=12month'); foreach ($xml->xpath('//artist/image[@size="medium"]') as $image) { echo $image . PHP_EOL; } Link to comment https://forums.phpfreaks.com/topic/185367-php-simplexml-help-with-attributes/#findComment-978730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.