ronniebrown Posted January 19, 2008 Share Posted January 19, 2008 Hi, I've got a test page up at http://ronniebrown.co.uk/parse_freshmeat.php. THis page is pulling in XML from Last.fm - http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml and I have PHP written to render it on screen. My problem is that only the "artist" is being brought back; neither of the other two elements (url, track) are rendered on screen. My PHP code is below. Any help much appreciated. <?php if( ! ($fp = fopen("http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml" , "r" )) ) die("Couldn't open xml file!"); $item_counter = 0; $in_item_tag = 0; $fm_current_tag_state = ''; $fm_headline_data = array(); function startElementHandler( $parser, $element_name, $element_attribs ) { global $item_counter; global $in_item_tag; global $fm_current_tag_state; global $fm_headline_data; if( $element_name == "TRACK" ) { $in_item_tag = 1; } if( $in_item_tag == 1 ) { $fm_current_tag_state = $element_name; } else { $fm_current_tag_state = ''; } } function endElementHandler( $parser, $element_name ) { global $item_counter; global $in_item_tag; global $fm_current_tag_state; global $fm_headline_data; $fm_current_tag_state = ''; if( $element_name == "TRACK" ) { $item_counter++; $in_item_tag = 0; } } function characterDataHandler( $parser , $data ) { global $item_counter; global $in_item_tag; global $fm_current_tag_state; global $fm_headline_data; if( $fm_current_tag_state == '' || $in_item_tag == 0 ) return; if( $fm_current_tag_state == "URL" ) { $fm_headline_data[$item_counter]["url"] = $data; } if( $fm_current_tag_state == "TRACK" ) { $fm_headline_data[$item_counter]["track"] = $data; } if( $fm_current_tag_state == "ARTIST" ) { $fm_headline_data[$item_counter]["artist"] = $data; } } if( !($xml_parser = xml_parser_create()) ) die("Couldn't create XML parser!"); xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler"); xml_set_character_data_handler( $xml_parser , "characterDataHandler" ); while( $data = fread($fp, 4096) ) { if( !xml_parse($xml_parser, $data, feof($fp)) ) { break; // get out of while loop if we're done with the file } } xml_parser_free($xml_parser); ?> <HTML> <HEAD> <TITLE>Music</TITLE> </HEAD> <BODY BGCOLOR="#ffffff"> <H3>Music</H3> <BR> <?php for( $i=0 ; $i < $item_counter ; ++$i ) { printf("<A HREF=\"%s\">%s</a> - %s<br>\n" , $fm_headline_data[$i]["url"] , $fm_headline_data[$i]["track"] , $fm_headline_data[$i]["artist"] ); } ?> </BODY> </HTML> Link to comment https://forums.phpfreaks.com/topic/86847-php-xml-help/ Share on other sites More sharing options...
Daniel0 Posted January 19, 2008 Share Posted January 19, 2008 If you're using PHP 5 (which you should be!) then I've written a tutorial which explains how to parse XML data using PHP: http://www.phpfreaks.com/tutorials/155/0.php It shouldn't be too hard to follow. Link to comment https://forums.phpfreaks.com/topic/86847-php-xml-help/#findComment-443873 Share on other sites More sharing options...
ronniebrown Posted January 19, 2008 Author Share Posted January 19, 2008 Hi Daniel, Thanks for the prompt reply. I took a look at the tutorial and came up with this page, which seems to reach the data ok but doesn't seem to loop through and return the results: http://ronniebrown.co.uk/test.php This is the (slightly) changed code: <?php $rss = new SimpleXMLElement('http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml', null, true); echo "<h1><a href='{$rss->track->artist}'>{$rss->track->artist}</a></h1>".PHP_EOL.'<hr />'.PHP_EOL; foreach($rss->xpath('track/artist') as $item) { echo <<<EOF <h2><a href='{$item->url}'>{$item->title}</a></h2> <div>Posted at: {$item->date}</div> {$item->track} <hr /> EOF; } ?> What am I doing wrong? Thanks Link to comment https://forums.phpfreaks.com/topic/86847-php-xml-help/#findComment-443881 Share on other sites More sharing options...
Daniel0 Posted January 20, 2008 Share Posted January 20, 2008 Try <?php $tracks = new SimpleXMLElement('http://ws.audioscrobbler.com/1.0/user/ronniebrown/recenttracks.xml', null, true); foreach($tracks as $track) { echo <<<EOF Name: {$track->name}<br /> Artist: {$track->artist}<br /> <a href='{$track->url}'>{$track->url}</a> <hr /> EOF; } ?> Link to comment https://forums.phpfreaks.com/topic/86847-php-xml-help/#findComment-444193 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.