View Full Version : xml in JavaScript

05-11-2009, 09:45 PM
I am looking to add an xml file to a JavaScript file.
The js file works like this:
When a page is called up by a user it will display a picture and text. The next time that user hits
the page it will display a different picture and text with it.

The xml file should work like this:
When the js. file calls on the xml file it should display a picture with the appropriate text.

The js. code is below.


//Start rotating
var gallery = new Array();
gallery[0] = new Array("XML WOULD BE PLACED HERE");

function pickImageFrom(whichGallery)
var idx = Math.floor(Math.random() * gallery[whichGallery].length);
document.write('<img src="'+gallery[whichGallery][idx]+'" border=0>');
//End Rotating

<SCRIPT type=text/javascript>pickImageFrom(0);document.write("<br>");</SCRIPT>


Old Pedant
05-11-2009, 11:38 PM
Why would you want or need XML in that array????

Would be much simpler to just have an array of arrays.

var gallery = new Array();
gallery[0] = ["fruit1.jpg","fruit2.jpg","fruit3.jpg"];
gallery[1] = ["carA.jpg","carB.jpg","carC.jpg","carD.jpg"];
gallery[2] = ["usa.jpg","mexico.jpg","canada.jpg","honduras.jpg","guatemala.jpg",...];

function pickImageFrom(whichGallery)
var g = gallery[whichGallery];
document.write('<img src="'+ g[Math.floor(Math.random()*g.length)] + '" border=0>');

If you think you *must* use XML for some reason, then at least show us what the XML looks like.

05-12-2009, 02:48 PM
I originally had code just like this with the images in the different arrays. Now I have to add text into the mix so the arrays become messier and harder to update. Below you will find the xml:

<?xml version="1.0" encoding="utf-8"?>



<image width="190" height="128"><![CDATA[<img src='http://www.com/image.jpg' border='1' />]]></image>
<title><![CDATA[<p><font color='#000000'><b>This is my header and the title goes here</b></font></p>]]></title>
<articles><![CDATA[<p>Les Écuyers du cercle St. Padre Pio 4992, de Henderson, au Nevada, emballent et vendent des programmes à la piste de course automobile de Las Vegas. Les Écuyers et des Chevaliers du conseil St. Francis of Assisi 13456, ainsi que les dames auxiliaires du conseil, ont rapporté 10 000 $ pour des œuvres de charité, grâce à la vente des ces programmes.&rdquo; </p>]]></articles>


Old Pedant
05-13-2009, 12:30 AM
So I assume that in actuality these XML streams will have more than one <item>? And that each <item> will have only one image?

Or is there only one <item> with multiple <image>s?

05-13-2009, 02:20 AM
If you want to parse xml in javascript you'd better use a javascript library like jquery it's small and it parses xml easily.

Old Pedant
05-13-2009, 04:29 AM
Or use the native XML parsers that are built into all modern browsers. They are even smaller, since they are already there.

Not really objecting to using JQuery, but isn't it way overkill for something this simple??

All he needs to do is get the appropriate XML loaded and then pick the requested <item> and then within that the requested <image>.


*POSSIBLY* as simple as this:

function pickImageFrom(whichGallery)
var g = gallery[whichGallery];
var xmlDoc;

try //Internet Explorer
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
} catch(e) {
try //Firefox, Mozilla, Opera, etc.
parser=new DOMParser();
} catch(e) {
alert("Can't find an XML DOM object: " + e.message)
var images = xmlDoc.getElementsByTagName("image");

Because his <image> nodes contain a complete HTML <img> tag, that's what he will get. If he wants only the src value from with in that tag, he'll have to further parse that out.