PDA

View Full Version : Accessing Difficult XML Node


oneal.michaels
06-25-2010, 09:11 PM
Document from Last.fm api... scroll down to read question...
<lfm status="ok">

<recenttracks user="RJ" page="1" perPage="10" totalPages="3168">

<track>

<artist mbid="bc641be9-ca36-4c61-9394-5230433f6646">Liquid Tension Experiment</artist>

<name>Biaxident</name>

<streamable>1</streamable>

<mbid/>

<album mbid="6c20d297-121e-47d0-aa3a-8f27c7a06553">Liquid Tension Experiment 2</album>

<url>http://www.last.fm/music/Liquid+Tension+Experiment/_/Biaxident</url>

<image size="small">http://userserve-ak.last.fm/serve/34s/33028463.jpg</image>

<image size="medium">http://userserve-ak.last.fm/serve/64s/33028463.jpg</image>

<image size="large">http://userserve-ak.last.fm/serve/126/33028463.jpg</image>

<image size="extralarge">http://userserve-ak.last.fm/serve/300x300/33028463.jpg</image>

<date uts="1274524554">22 May 2010, 10:35</date>

</track>

....more tracks

</recenttracks>

</lfm>

Here is the document i am working with from the last.fm data api, the complete file
is here (http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=rj&api_key=b25b959554ed76058ac220b7b2e0a026), and i am trying to access this node
<image size="small">http://userserve-ak.last.fm/serve/34s/33028463.jpg</image>
but i can;t seem to find out how because of the other three nodes with the same name,
if anyone could help me out with this it would be great,
i have been stumped for months, gave up for a while and decided that i cant leave anything unfinished.
-thanks, Mike

oneal.michaels
06-25-2010, 11:10 PM
I also forgot to mention that i am using javascript and the getElementsByTagName() function if that helps at all

Dormilich
06-26-2010, 08:17 AM
what about getting the <image> nodes and then checking for the size attribute?

oneal.michaels
06-26-2010, 05:07 PM
okay, so i posted this up on another website and got this as a response


var images = document.getElementsByTagName("image");
for (var i = 0; i < images.length; ++i)
{
if (images[i].getAttribute("size") != null && images[i].getAttribute("size") == "small")
{
//do your thing
}
}


which worked but now i am trying to access the value inside the node with

imgURL = images[i].childNodes[0].nodeValue;

and it is returning an "Object Required" error

oneal.michaels
06-26-2010, 05:27 PM
soo i got some input from another forum, and i improved on what everyone gave me and came up with this


var imgURL = "http://i45.tinypic.com/f3w5lk.jpg"; //picture for no album art
var images = xmlDoc.getElementsByTagName("image");
for (var i = 0; i < images.length; ++i)
{
if (images[i].getAttribute("size") != null && images[i].getAttribute("size") == "extralarge" && images[i].firstChild)
{
imgURL = images[i].firstChild.nodeValue;
}
}


i also decided to go with the higher quality picture from the "extralarge" image node, it makes my app look much prettier

oneal.michaels
06-26-2010, 10:42 PM
if anyone was wondering what i was working on you can check it out here (http://www.myspace.com/last.fm), as of now it is not published so you will not be able to see it unless you add me on myspace (heres (http://www.myspace.com/omichael08) my profile) and i add you as an application tester, so if you want just add me and shoot me a message and i will let you check it out, and hopefully it will be processed and published soon