Javascript for-loop不循环

时间:2015-10-21 10:28:52

标签: javascript xml loops for-loop

一个常见的问题但很明显为什么我的循环不起作用?我做了什么错误,任何帮助将不胜感激:)

XML

<?xml version="1.0" encoding="utf-8"?>
<School>
  <child>
    <name>john</name>
    <dob>28-09-2000</dob>
    <age>15</age>
    <add>jgdkhcjgvbv</add>
    <phone>12345678</phone>
  </child>
  <child>
    <name>henry</name>
    <dob>19-09-2001</dob>
    <age>14</age>
    <add>kjdfndsjnlae</add>
    <phone>23456789</phone>
  </child>
  <child>
    <name>luke</name>
    <dob>27-04-1999</dob>
    <age>16</age>
    <add>ksmfodnzsksl</add>
    <phone>34567890</phone>
  </child>
</School>

HTML

<html>
<head>
<script type="text/javascript" >
    var xmlObject, docObject, childarray, cname, cdob, cage;
    xmlObject = new XMLHttpRequest();
    xmlObject.open("GET","school.xml",false);
    xmlObject.send();
    docObject = xmlObject.responseXML;
    childarray=docObject.getElementsByTagName("child");

    for (var i = 0; i<childarray.length; i++) {
        cname = docObject.getElementsByTagName("name")[i].childNodes[0].nodeValue;
        document.write("Name : " + cname + "<BR/>");

        cdob = docObject.getElementsByTagName("dob")[i].childNodes[0].nodeValue;
        document.write("Dob : " + cdob + "<BR/>");

        cage = docObject.getElementsByTagName("age")[i].childNodes[0].nodeValue;
        document.write("Age : " + cage + "<BR/>");

        cadd = docObject.getElementsByTagName("add")[i].childNodes[0].nodeValue;
        document.write("Add : " + cadd + "<BR/>");

        cphone = docpb.getElementsByTagName("phone")[i].childNodes[0].nodeValue;
        document.write("Phone : " + cphone + "<BR/>");

    }
</script>
<title>school</title>
</head>
<body>
</body>
</html>

我想要做的是打印姓名,dob,年龄,地址,电话以及&#39; n&#39;一次学生人数。该代码仅显示第一个孩子的详细信息。

Welp

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加&#34; =&#34;在

var i = 0; I&LT = childarray.length; i ++