PDA

View Full Version : Troubles with parsing XML document



Steez
March 23rd, 2007, 10:45 AM
Hello there,

I'm new here and i have a question about an articele on this site.
I fount this article aboutXML Parsing using PHP {Intermediate} (http://www.kirupa.com/web/xml_php_parse_intermediate.htm) by google.
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 (http://ws.audioscrobbler.com/1.0/user/Maurice-k/weeklyartistchart.xml) 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. &ndash; Music at Last.fm">T.I.</a> - 58</li>
<li><a href="http://www.last.fm/music/T.I." title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="Jedi Mind Tricks &ndash; Music at Last.fm">Jedi Mind Tricks</a> - 56</li>
<li><a href="http://www.last.fm/music/Jedi+Mind+Tricks" title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="The Game &ndash; Music at Last.fm">The Game</a> - 35</li>
<li><a href="http://www.last.fm/music/The+Game" title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="Juelz Santana &ndash; Music at Last.fm">Juelz Santana</a> - 31</li>
<li><a href="http://www.last.fm/music/Juelz+Santana" title=" &ndash; Music at Last.fm"></a> - </li>
<li><a href="" title="Wolfgang Amadeus Mozart &ndash; Music at Last.fm">Wolfgang Amadeus Mozart</a> - 27</li>
<li><a href="http://www.last.fm/music/Wolfgang+Amadeus+Mozart" title=" &ndash; 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>\n");
for($x=0;$x<10;$x++){
echo("<li><a href=\"".$story_array[$x]->url."\" title=\"".$story_array[$x]->name." &ndash; Music at Last.fm\">".$story_array[$x]->name."</a> - ".$story_array[$x]->playcount."</li>\n");
}
?>
</ol>

I hope anyone sees the problem cuz i'm pretty much freakin'out now!

Thanks!

Steez
March 25th, 2007, 09:32 AM
Nobody an idea? :puzzled:

I think it should'nt be that difficult but i can't make it work....

Steez
March 28th, 2007, 12:27 PM
* * Little kick * *