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.