Hello there,
I’m new here and i have a question about an articele on this site.
I fount this article about[FONT=Verdana][SIZE=2][COLOR=#003366][COLOR=Black]XML Parsing using PHP {Intermediate} by google[/COLOR][/COLOR][/SIZE][/FONT].
The articele is pretty good! Did’nt found anything like it elsewere, thanks fot that!
But i have a problem. I use the code on the page to display my weekly Last.fm charts on my website, but for that i need to get another value from the XML document. I need the URL of the artistpage on last.fm. I modified the script a little but assuming that this should work but instead i get this output:
<ol>
<li><a href="" title="T.I. – Music at Last.fm">T.I.</a> - 58</li>
<li><a href="http://www.last.fm/music/T.I." title=" – Music at Last.fm"></a> - </li>
<li><a href="" title="Jedi Mind Tricks – Music at Last.fm">Jedi Mind Tricks</a> - 56</li>
<li><a href="http://www.last.fm/music/Jedi+Mind+Tricks" title=" – Music at Last.fm"></a> - </li>
<li><a href="" title="The Game – Music at Last.fm">The Game</a> - 35</li>
<li><a href="http://www.last.fm/music/The+Game" title=" – Music at Last.fm"></a> - </li>
<li><a href="" title="Juelz Santana – Music at Last.fm">Juelz Santana</a> - 31</li>
<li><a href="http://www.last.fm/music/Juelz+Santana" title=" – Music at Last.fm"></a> - </li>
<li><a href="" title="Wolfgang Amadeus Mozart – Music at Last.fm">Wolfgang Amadeus Mozart</a> - 27</li>
<li><a href="http://www.last.fm/music/Wolfgang+Amadeus+Mozart" title=" – Music at Last.fm"></a> - </li>
</ol>
While the url should come just in the href attribute. What did i do wrong? I don’t see it anymore. I now have this code:
<?php
$xml_file = "http://ws.audioscrobbler.com/1.0/user/Maurice-k/weeklyartistchart.xml";
$xml_artist_name_key = "*WEEKLYARTISTCHART*ARTIST*NAME";
$xml_artist_play_key = "*WEEKLYARTISTCHART*ARTIST*PLAYCOUNT";
$xml_artist_url_key = "*WEEKLYARTISTCHART*ARTIST*URL";
$story_array = array();
$counter = 0;
class xml_story{
var $name, $play, $url;
}
function startTag($parser, $data){
global $current_tag;
$current_tag .= "*$data";
}
function endTag($parser, $data){
global $current_tag;
$tag_key = strrpos($current_tag, '*');
$current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
global $current_tag, $xml_artist_name_key, $xml_artist_play_key, $xml_artist_url_key, $counter, $story_array;
switch($current_tag){
case $xml_artist_name_key:
$story_array[$counter] = new xml_story();
$story_array[$counter]->name = $data;
break;
case $xml_artist_play_key:
$story_array[$counter]->playcount = $data;
$counter++;
break;
case $xml_artist_url_key:
$story_array[$counter]->url = $data;
$counter++;
break;
}
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($xml_file, "r") or die("Could not open file");
$data = fread($fp, 4096) or die("Could not read file");
if(!(xml_parse($xml_parser, $data))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
echo("<ol>
");
for($x=0;$x<10;$x++){
echo("<li><a href=\"".$story_array[$x]->url."\" title=\"".$story_array[$x]->name." – Music at Last.fm\">".$story_array[$x]->name."</a> - ".$story_array[$x]->playcount."</li>
");
}
?>
</ol>
I hope anyone sees the problem cuz i’m pretty much freakin’out now!
Thanks!