Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    xml in JavaScript

    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.


    Chris
    [CODE]

    //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>


    [CODE]

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Why would you want or need XML in that array????

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

    Code:
    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.

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:

    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <NewsAndEvents>
    
    <title><![CDATA[<b>Header</b>]]></title>
    
    <item>
    <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>
    </item>
    
    
    
    </NewsAndEvents>
    Last edited by cjlaviola; 05-12-2009 at 08:20 PM.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    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?

  • #5
    Regular Coder
    Join Date
    Oct 2007
    Posts
    277
    Thanks
    2
    Thanked 4 Times in 4 Posts
    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    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>.

    No?

    *POSSIBLY* as simple as this:
    Code:
    function pickImageFrom(whichGallery)
    {
       var g = gallery[whichGallery];
       var xmlDoc;
    
       try //Internet Explorer
       { 
           xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
           xmlDoc.async="false";
           xmlDoc.loadXML(g);
       } catch(e) {
           try //Firefox, Mozilla, Opera, etc.
           {
               parser=new DOMParser();
               xmlDoc=parser.parseFromString(g,"text/xml");
           }   catch(e) {
               alert("Can't find an XML DOM object: " + e.message)
               return;
           }
       }
       var images = xmlDoc.getElementsByTagName("image");
       document.write(images[Math.floor(Math.random()*images.length)].nodeValue);
    }
    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.
    Last edited by Old Pedant; 05-13-2009 at 03:40 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •