Jump to content

PHP SimpleXML Help with Attributes


johnslater

Recommended Posts

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

<?
$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.

<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.

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];
        }
    }
  }
?>

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.