Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6565589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:06:45+00:00 2026-05-25T14:06:45+00:00

I will keep this very short: I’m trying to make a loop through an

  • 0

I will keep this very short:
I’m trying to make a loop through an xml-document for a gallery. I got a script that should work, but doesn’t. Can anyone please tell me where I did wrong?

I didn’t want to make this longer because the problem is simple and have been pondering over this since yesterday and this is the closest I get.

I want to loop through the xml-file and print out “path” and “file” first and most. I’m building a gallery and thought that the best way to save all the data for the images was an xml-file, but now I can’t get it to loop correctly. In the script I made the page print out both x and i, which resulted with x being 1 and i being 0, hence it hasn’t worked through the for-loop at all as I see it.

Any help would be appreciated, because I’m stuck. Been trying so many solutions that my head is spinning and I can’t get any further without a nudge in the right direction.

The html/javascript:

        <script type="text/javascript">
            function displayResult()
            {
                if (window.XMLHttpRequest)
                {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                }
                else
                {// code for IE6, IE5
                    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                }
                xmlhttp.open("GET","../gallery/gallery.xml",false);
                xmlhttp.send();
                xmlDoc=xmlhttp.responseXML;


                x=xmlDoc.getElementsByTagName("session");
                for (i=0;i<x.length;i++)
                { 
                    img = "<img src='";
                    path = (x[0].getElementsByTagName("path")[0].childNodes[0].nodeValue);
                    file = (x[i].getElementsByTagName("file")[i].childNodes[0].nodeValue);
                    end = "' /><br />";
                    name = (x[i].getElementsByTagName("name")[i].childNodes[0].nodeValue);
                    txt = "x:" + x.length + "| i " + i + "<br />" + img + path + file + end + name + "<br />";
                    document.getElementById("content").innerHTML = txt;
                    //document.write(txt);
                }
            }
        </script>
    </head>
    <body onload="displayResult()">
        <div id='content'></div>
    </body>
</html>

xml-file:

<gallery>
    <session>
        <path>../gallery/beauty/</path>
        <item>
            <file>_DSC2331.jpg</file>
            <name>Picture 1</name>
        </item>
        <item>
            <file>_DSC2339.jpg</file>
            <name>Picture 2</name>
        </item>
        <item>
            <file>_DSC2350.jpg</file>
            <name>Picture 3</name>
        </item>
        <date>2011-08-03</date>
    </session>
</gallery>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:06:46+00:00Added an answer on May 25, 2026 at 2:06 pm

    If I can make some suggestions:

    1. Use the var keyword within functions to make those variables local to that function. The code you have at the moment would set values in the global namespace, which is often considered bad practice (e.g. you could overwrite other people’s variables, or other people could overwrite yours). Also declare your variables at the start of a function, as they will be hoisted there anyway.
    2. Split your code up into more meaningful functions. This way they become easier to read and often then become more reusable.
    3. Make sure you loop through items as well as sessions.
    4. Consider using a Javascript framework like jQuery. They can often simplify the code you have to write, and you will usually end up writing less code yourself.

    .

    <html>
        <head>
            <script type="text/javascript">
                function loadDoc(url) {
                    var xmlhttp = null;
                    if (window.XMLHttpRequest) {
                        // code for IE7+, Firefox, Chrome, Opera, Safari
                        xmlhttp = new XMLHttpRequest();
                    } else {
                        // code for IE6, IE5
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    xmlhttp.open("GET", url, false);
                    xmlhttp.send();
                    return xmlhttp.responseXML;
                }
    
                function getContent(sessions) {
                    var items = null,
                        i = 0,
                        j = 0,
                        img = "",
                        path = "",
                        file = "",
                        end = "",
                        name = "",
                        txt = "";
                    for (i = 0; i < sessions.length; i++) { 
                        items = sessions[i].getElementsByTagName("item");
                        path = sessions[i].getElementsByTagName("path")[0].childNodes[0].nodeValue;
                        for (j = 0; j < items.length; j++) {
                            img = "<img src='";
                            file = items[j].getElementsByTagName("file")[0].childNodes[0].nodeValue;
                            end = "' /><br />";
                            name = items[j].getElementsByTagName("name")[0].childNodes[0].nodeValue;
                            txt += "session[" + i + "] item[" + j + "]<br />" + img + path + file + end + name + "<br />";
                        }
                    }
                    return txt;
                }
    
                function displayResult()
                {
                    var xmlDoc = loadDoc("../gallery/gallery.xml");
                    var sessions = xmlDoc.getElementsByTagName("session");
                    var txt = getContent(sessions);
                    document.getElementById("content").innerHTML = txt;
                }
            </script>
        </head>
        <body onload="displayResult()">
            <div id='content'></div>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a custom script that will keep a list of
Would anyone happen to know a trick that will keep this MSBuild task from
I will try to keep this as simple as possible. I have a rather
In various projects there are certain parts I will keep jumping to. Is there
When using TempData, my understanding is that it will keep whatever you put in
I am creating a database that will help keep track of which employees have
Will using GetComInterfaceForObject and passing the returned IntPtr to unmanaged code keep the managed
specifically, will working with containers as opposed to the static ObjectFactory let me keep
My question is very simple. I will ask it first in case the answer
I have put together a script which is very much like the flickr photostream

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.