FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


xml parsing





woodenbrick
It's been a while since i've done any coding in php, been learning java recently and I guess it's all getting messed up in my head. I had the idea of parsing .xml files from last.fm and making various graphs of my listening habits etc. but ran into a couple of problems:

#1: I have an array of dates in unix time but when I try to echo them using the date function I get this error:
Code:
Warning: date() expects parameter 2 to be long, object given in D:\brick\www\xmlparsing.php on line 42


#2: The following code should print out the playcount of an artist if it is in my $top10Artists array. For some reason $t never equals $a->name.

Code:
$user = "woodenbrick";
$url = "http://ws.audioscrobbler.com/1.0/user/$user";
$topArtistsOverall = "$url/topartists.xml";
$weeklyArtists = "$url/weeklyartistchart.xml";
$chartlists = "$url/weeklychartlist.xml";

//get top 10 current artists
$top10Artists = array();
$xml = simplexml_load_file($topArtistsOverall) or die ("Couldnt load $topArtistsOverall");
for($x = 0; $x < 10; $x++){
   $top10Artists[] = $xml->artist[$x]->name;
}

//get list of chart dates
$xml = simplexml_load_file($chartlists) or die("Couldnt load $chartlists");
$from = array();
$to = array();
foreach($xml->chart as $chart){
   $from[] = $chart['from'];
   $to[] = $chart['to'];
}

//this part of the script will take some time to execute, since only
//allowed to access audioscrobbler once per second apparently(52 seconds for a year of charts)

for($x=0; $x<sizeof($from); $x++){
   echo "<p />Charts from ".date("d m y", $from[$x])." to ".date("d m y", $to[$x])."<br />";//error #1 here
   $currentXml = "$weeklyArtists?from=".$from[$x]."&to=".$to[$x];
   $xml = simplexml_load_file($currentXml) or die("couldnt load $currentXml");
   foreach($xml->artist as $a){
      foreach($top10Artists as $t){
         if($t == $a->name){
            echo "$a->name Playcount: $a->playcount<br />";//error #2 here
         }
      }
         
      //}
      
   }
   sleep(1);
}
woodenbrick
Ok, I figured out the first error by explicitly casting the array variables as integers, any ideas on the second?

Edit: nevermind, same problem, for some reason xml file data needs to be explicitly cast to be used...
SlowWalkere
woodenbrick wrote:
Edit: nevermind, same problem, for some reason xml file data needs to be explicitly cast to be used...

Yeah, I've run into these problems as well.

With SimpleXML, each node (each piece of data) is represented by a SimpleXML Object. Even if an item has no children - i.e. it's just text (like the name) - it's an object. The objects are designed to output the text value when they're accessed, but it doesn't do the same thing for making logical comparisons.

So, as you found out, you want to typecast the value to whatever it should be - int or string - before you make any comparisons.

- Walkere
Related topics
TrendyFlash Site Builder 1.0
Should I make a flash site?
Help making an AJAX messaging system
RSS Help PLease
php and am lost...
Validating
Can't download files after a Firefox auto update
ASPX XML parsing error help
rss 2.0 feeds
XML parsing
a regex help
Parse feed tags/urls using rss2html
Creating XML files using PHP
PHP XML Parsing
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.