Newb question involving DOMDocument and XPath queries
Posted: Fri Apr 25, 2008 3:45 am
Hello,
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:
I'm using XPath for this, in conjunction with the DOMDocument object. Problem is, when I execute this PHP code
The artist name is printed twice (eg. "The KillersThe Killers"). This could be a problem with the XPath query, but I don't think so. I'm thinking the DOMNodeList returned by the query($query) function call is the culprit.
How can I modify the above code to only print the artist name once? Help is greatly appreciated.
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.