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

    Attempting to use JS to parse info from XML into HTML

    Code:
    <!--
    var xmlDoc=null;
    if (window.ActiveXObject)
    {// code for IE
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    }
    else if (document.implementation.createDocument)
    {// code for Mozilla, Firefox, Opera, etc.
    xmlDoc=document.implementation.createDocument("","",null);
    }
    else
    {
    alert('Your browser cannot handle this script');
    }
    if (xmlDoc!=null)
    {
    xmlDoc.async=false;
    xmlDoc.load("test.xml");
    
    document.write("<table width='74' border='0'>");
    
    var x=xmlDoc.getElementsByTagName("ENTRY");
    for (i=0; i<x.length; i++) {
    document.write("<tr>");
    document.write("<td colspan='3'>");
    document.write("<a href='");
    document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
    document.write("'>");
    document.write("<img src='");
    document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
    document.write("' alt='");
    docuemnt.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
    docuemnt.write("' />");
    document.write("</a>");
    document.write("</td>");
    document.write("</tr>");
    
    /*document.write("<tr>");
    document.write("<td>");
    document.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
    document.write("</td>");
    //document.write("</tr>");
    //document.write("<tr>");
    document.write("<td>");
    document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
    document.write("</td>");
    //document.write("</tr>");
    //document.write("<tr>");
    document.write("<td>");
    document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
    document.write("</td>");
    document.write("</tr>");*/
    }
    document.write("</table>");
    }
    //-->
    There may be an easier way to do this but I am unaware of it.

    Also you will notice it is actually different scripts.

    One is an attempt at the final product, to get a image to load with a link and alt description.

    The other works fine and loads the 3 sections of infomation into a table so I can see that it is querying the XML file correctly. I have attached the .js file and the .xml file.

    I appreciate any assistance that can be rendered
    Attached Files Attached Files

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,028
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    So what's the question??

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My question is:

    The script works correctly when it is only doing with text,

    Code:
    for (i=0; i<x.length; i++) {
    document.write("<tr>");
    document.write("<td>");
    document.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
    document.write("</td>");
    //document.write("</tr>");
    //document.write("<tr>");
    document.write("<td>");
    document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
    document.write("</td>");
    //document.write("</tr>");
    //document.write("<tr>");
    document.write("<td>");
    document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
    document.write("</td>");
    document.write("</tr>");
    }
    but when I attempt to implement with images,

    Code:
    for (i=0; i<x.length; i++) {
    document.write("<tr>");
    document.write("<td>");
    document.write("<a href='");
    document.write(x[i].getElementsByTagName("LINK")[0].childNodes[0].nodeValue);
    document.write("'>");
    document.write("<img src='");
    document.write(x[i].getElementsByTagName("IMG")[0].childNodes[0].nodeValue);
    document.write("' alt='");
    docuemnt.write(x[i].getElementsByTagName("MFG")[0].childNodes[0].nodeValue);
    docuemnt.write("' />");
    document.write("</a>");
    document.write("</td>");
    document.write("</tr>");
    }
    All i get is the first image. Are there any sugestions on how I may have made an error that might be causing this? Or perhaps there is a better way to do the same thing?


  •  

    Posting Permissions

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