Click here to Skip to main content
15,298,916 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone

I am working on a project where I am loading data from xml file to html using javascript.

What I have tried:

Javascript code

        var xmlhttp, xmlDoc;
        xmlhttp = new XMLHttpRequest();"GET", "books.xml", false);
        xmlDoc = xmlhttp.responseXML;
        x = xmlDoc.getElementsByTagName("book")[0].childNodes;
        z = xmlDoc.getElementsByTagName("book")
        for (k = 0; k < x.length; k++) {          
                y = xmlDoc.getElementsByTagName("book")[k].firstChild;
            for (i = 0; i < x.length; i++) //looping xml childnodes
                if (y.nodeType == 1) {
                    document.write(y.nodeName + ":" + y.firstChild.nodeValue + "<br />");
                y = y.nextSibling;

xml file[^]


title:Everyday Italian
author:Giada De Laurentiis
title:Harry Potter
author:J K. Rowling
title:XQuery Kick Start
author:James McGovern
author:Per Bothner
author:Kurt Cagle
title:Learning XML
author:Erik T. Ray

As it is seen, I have lost data from the third node, what may be the mistake.
Also is it possible to skip a node in a loop.

Please help

Thanks in advance.
Updated 24-Feb-16 8:31am

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^][^][^]

With the debugger, you will be able to see exact how your code is handling the third node, and probably understand why it fail.

To skip a book, you just have to put a test in the loop that scan the books tour test will say which book you skip.
The error is pretty obvious: both loops are limited to x.length iterations.

What is x? It's the child nodes of the first book element.

The first book element contains four child nodes. As a result, you attempt to read four book elements - that happens to be the right number, but only accidentally - and you attempt to read four child elements of each book node. Books 1, 2 and 4 have four child elements, but book 3 has eight child elements.

Fix your loops and the code will work properly, regardless of how many books there are, and how many child elements they have:
xmlDoc = xmlhttp.responseXML;

var z = xmlDoc.getElementsByTagName("book");
for (var k = 0; k < z.length; k++) {
    var x = z[k].childNodes;
    for (var i = 0; i < x.length; i++) {
        var y = x[i];
        if (y.nodeType == 1) {
            document.write(y.nodeName + ":" + y.firstChild.nodeValue + "<br />");
JPais 1-Mar-16 22:35pm
Thank you so much for your answer..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900