Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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