I'm relatively new to PHP and have a simple question.
I'm trying print the name of the first artist in an XML file. Here's and excerpt of the file:
Code: Select all
<recenttracks>
<track streamable="true">
<artist mbid="95e1ead9-4d31-4808-a7ac-32c3614c116b">The Killers</artist>
<name>Read My Mind</name>
<mbid></mbid>
<album mbid="">Sam's Town</album>
<url>http://www.last.fm/music/The+Killers/_/Read+My+Mind</url>
<date uts="1209110890">25 Apr 2008, 08:08</date>
</track>
<track streamable="true">
<artist mbid="8538e728-ca0b-4321-b7e5-cff6565dd4c0">Depeche Mode</artist>
<name>Everything Counts</name>
<mbid></mbid>
<album mbid="b8cdaddb-d19e-41fa-8b93-fba58d160d4b">Construction Time Again</album>
<url>http://www.last.fm/music/Depeche+Mode/_/Everything+Counts</url>
<date uts="1209110649">25 Apr 2008, 08:04</date>
</track>
</recenttracks>
Code: Select all
$doc = new DOMDocument();
$doc->load("http://ws.audioscrobbler.com/1.0/user/$username/recenttracks.xml");
$xpath = new DOMXPath($doc);
$query = "/recenttracks/track[1]/artist";
$lastplayedtracks = $xpath->query($query);
echo $lastplayedtracks->item(0)->nodeValue;
How can I modify the above code to only print the artist name once? Help is greatly appreciated.