Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Parsing

Status
Not open for further replies.

Tjcrazy

Programmer
Feb 4, 2010
2
GB
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
Code:
[URL unfurl="true"]http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks&user=bbcradio1&api_key=c910b0bc1e2d4e64964ebcd2d69c255c&limit=500[/URL]

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
 
Hi

Tim said:
One of [red]my[/red] XML files is located at
If yours means you made it, then I would do a favor to myself and use another format. ( There is no secret : I hate XML. )

If it must be XML and JavaScript, then the answer should be : use Sarissa. It is quite strong and flexible, but not accomplished all my wishes. But that was a couple of years ago.

As a quick and painless solution, I would try to access the tags the DOM way. But if you have much bigger files too, you will wish to search for an Expat parser. It is faster than DOM parsers, but not available natively in browsers.

Supposing the XML file is available through an [tt]iframe[/tt] like this :
HTML:
[b]<iframe[/b] [maroon]src[/maroon][teal]=[/teal][green][i]"/2.0/?method=user.getrecenttracks&user=bbcradio1&api_key=c910b0bc1e2d4e64964ebcd2d69c255c&limit=500"[/i][/green] [maroon]id[/maroon][teal]=[/teal][green][i]"xml"[/i][/green][b]></iframe>[/b]
This code :
JavaScript:
[b]var[/b] doc[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'xml'[/i][/green][teal]).[/teal]contentDocument
[b]var[/b] track[teal]=[/teal]doc[teal].[/teal][COLOR=darkgoldenrod]getElementsByTagName[/color][teal]([/teal][green][i]'track'[/i][/green][teal])[[/teal][purple]0[/purple][teal]][/teal]
[b]var[/b] name[teal]=[/teal]track[teal].[/teal][COLOR=darkgoldenrod]getElementsByTagName[/color][teal]([/teal][green][i]'name'[/i][/green][teal])[[/teal][purple]0[/purple][teal]].[/teal]textContent
[b]var[/b] artist[teal]=[/teal]track[teal].[/teal][COLOR=darkgoldenrod]getElementsByTagName[/color][teal]([/teal][green][i]'artist'[/i][/green][teal])[[/teal][purple]0[/purple][teal]].[/teal]textContent
[b]var[/b] image[teal]=[/teal]track[teal].[/teal][COLOR=darkgoldenrod]getElementsByTagName[/color][teal]([/teal][green][i]'image'[/i][/green][teal])[/teal]
[b]var[/b] large
[b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]image[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [b]if[/b] [teal]([/teal]image[teal][[/teal]i[teal]].[/teal][COLOR=darkgoldenrod]getAttribute[/color][teal]([/teal][green][i]'size'[/i][/green][teal])==[/teal][green][i]'large'[/i][/green][teal])[/teal] large[teal]=[/teal]image[teal][[/teal]i[teal]].[/teal]textContent
[COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'Name : '[/i][/green][teal]+[/teal]name[teal]+[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Artist : '[/i][/green][teal]+[/teal]artist[teal]+[/teal][green][i]'[/i][/green][lime][i]\n[/i][/lime][green][i]Large : '[/i][/green][teal]+[/teal]large[teal])[/teal]
Will display this :
Code:
Name : Star Girl
Artist : McFly
Large : [URL unfurl="true"]http://userserve-ak.last.fm/serve/126/35394581.png[/URL]
In Gecko, Presto, KHTML and WebKit based browsers. Not tested with Trident.

Using the Selector API would make the JavaScript code simpler. Supposing the HTML part is the same, the previous [tt]alert()[/tt] would display the same text after this lines too :
JavaScript:
[b]var[/b] doc[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'xml'[/i][/green][teal]).[/teal]contentDocument
[b]var[/b] name[teal]=[/teal]doc[teal].[/teal][COLOR=darkgoldenrod]querySelector[/color][teal]([/teal][green][i]'track name'[/i][/green][teal]).[/teal]textContent
[b]var[/b] artist[teal]=[/teal]doc[teal].[/teal][COLOR=darkgoldenrod]querySelector[/color][teal]([/teal][green][i]'track artist'[/i][/green][teal]).[/teal]textContent
[b]var[/b] large[teal]=[/teal]doc[teal].[/teal][COLOR=darkgoldenrod]querySelector[/color][teal]([/teal][green][i]'track image[size=large]'[/i][/green][teal]).[/teal]textContent
In Gecko, Presto and WebKit based browsers. Not implemented yet in KHTML. Not tested with Trident.


Feherke.
 
I tried the code out on my own browser, it didn't seem to work. Not sure why.
Some people have said it is because the XML file i not on my own server?
 
Hi

Tjcrazy said:
Some people have said it is because the XML file i not on my own server?
You forgot to mention that "minor" detail. They are right, JavaScript can not access the content of an [tt]iframe[/tt] if that content was loaded from a different domain.

I would take a look at thread215-1589249 , ( especially my post at 4 Feb 10 4:33 ).

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top