XML Parsing problem with JS

Hey,

Im fairly new to javascript but I am an experianced php programmer. I have been parsing XML information using php, but my CPU usage has been too high recently due to this.

I am trying to convert my scripts to javascript, but I am unsure how I can do this using javascript. I think that if I get started off, the rest will be easy.

One of my XML files is located at

http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=bbcradio1&api_key=c910b0bc1e2d4e64964ebcd2d69c255c&limit=500

Basically I have been trying to grab the Name, Artist and large image of the most recent <track>.

Any help would be greatly appreciated and linked too on my website ;).

Tim

Why are you trying to do this with JS?

Parsing this XML document depends on how you intend to access it. You can’t do it via Ajax because the XML file is on a different domain (I doubt you are going to be doing this from the audioscrobbler.com domain). So since you’d have to grab the XML on the server side, you might as well process it there too.

Technically you could access the XML via a hidden iframe but it’s not what I’d consider a “clean” solution.

Wow thanks, thats loads of help.

Say what if I got the xml from my own domain (Fetched from audioscrobbler via php)?

What would I dio then to parse the xml file using JS?

Well then I would recommend doing it all with PHP! PHP has lots of XML parsing functions - head over to http://php.net and look up the XML functions. It even has DOMDocument which allows you to parse it much like you would parse a normal HTML or XML DOM via javascript.

But, if for whatever reason you have to do it with JS, you would just launch a simple XMLHttpRequest for the XML file and then parse it, using responseXML (not responseText). If you don’t know what that means, look for any simple Ajax tutorial and follow it - there are probably thousands of Ajax tutorials out there.