如何在javascript中循环遍历xml文件并跳过循环中的节点 [英] How to loop through xml file in javascript and skip a node in a loop

查看:66
本文介绍了如何在javascript中循环遍历xml文件并跳过循环中的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在开发一个项目,我正在使用javascript将数据从xml文件加载到html。



我尝试过:



Javascript代码



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

<script>
        var xmlhttp, xmlDoc;
        xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", "books.xml", false);
        xmlhttp.send();
        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;
                
            }
            document.write("-------------------<br/>");
        }
    </script>





xml文件



http://www.w3schools.com/xsl/books.xml [ ^ ]



输出:





xml file

http://www.w3schools.com/xsl/books.xml[^]

Output:

title:Everyday Italian
author:Giada De Laurentiis
year:2005
price:30.00
-------------------
title:Harry Potter
author:J K. Rowling
year:2005
price:29.99
-------------------
title:XQuery Kick Start
author:James McGovern
author:Per Bothner
author:Kurt Cagle
-------------------
title:Learning XML
author:Erik T. Ray
year:2003
price:39.95
-------------------





如图所示,我丢失了第三个节点的数据,可能是错误。

也可以跳过循环中的节点。



请帮助



提前致谢。



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.

推荐答案

您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



使用调试器,您将能够确切地看到代码处理第三个节点的确切方式,并可能理解它失败的原因。




要跳过一本书,你只需要在循环中进行测试,扫描书籍巡视测试会说你跳过哪本书。
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[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

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

[edit]
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.


错误很明显:两者都是循环仅限于 x.length 迭代。



什么是 x ?它是第一本书元素的子节点。



第一本书元素包含四个子节点。因此,您尝试阅读四个书元素 - 发生是正确的数字,但只是偶然 - 并且您尝试读取每个书籍节点的四个子元素。第1册,第2册和第4册有4个子元素,但第3册有8个子元素。



修复你的循环,代码将正常工作,无论有多少本书他们有多少子元素:

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 />");
        }
    }
    document.write("-------------------<br/>");
}


这篇关于如何在javascript中循环遍历xml文件并跳过循环中的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆