Need help looping through an xml file with javascript

时间:2022-08-26 11:28:46

This is my first time working with ajax in any way so please bear with me.

这是我第一次以任何方式使用ajax,所以请耐心等待。

In my program I have a drop down boxes that will dynamically pull zip codes, counties and cities depending on the state that is selected. So far the counties are working correctly, but the zip codes and cities are only showing the first record on my xml sheet.

在我的程序中,我有一个下拉框,可根据所选的状态动态提取邮政编码,县和城市。到目前为止,县的工作正常,但邮政编码和城市只显示我的xml表上的第一个记录。

The xml looks something like this:

xml看起来像这样:

<states>
   <counties>
     <county>
       <countyid>id#1</countyid>
       <countyname>nassau</countyname>
     </county>
   </counties>
   <zipcodes>
     <zip>10109</zip>
   </zipcodes>
   <cities>
     <city>New York</city>
   <cities>
</states>

Now, the javascript that loops through the counties section looks like this:

现在,循环遍历县部分的javascript如下所示:

target1.options[0] = new Option("Select County", "null");
for (var i = 0; i < xmlCounties.length; i++) {
target1.options[target1.options.length] = new Option(xmlCounties[i].childNodes[1].firstChild.nodeValue, xmlCounties[i].childNodes[0].firstChild.nodeValue, false, (matched == xmlCounties[i].childNodes[0].firstChild.nodeValue));

}

}

That works just fine, but for the cities and zip codes they don't, which both look like this and are identical to the above example:

这很好,但是对于城市和邮政编码他们没有,这看起来像这样,并且与上面的例子相同:

target2.options[0] = new Option("Select Zipcode", "null");
for (var i = 0; i < xmlZips.length; i++) {target2.options[target2.options.length] = new Option(xmlZips[i].childNodes[0].firstChild.nodeValue, xmlZips[i].childNodes[0].firstChild.nodeValue, false, (matched == xmlZips[i].childNodes[0].firstChild.nodeValue));
}

They both pull data from the xml, but only the first record. Any ideas on how to get this fixed? Thanks!

它们都从xml中提取数据,但只从第一条记录中提取数据。有关如何解决此问题的任何想法?谢谢!

1 个解决方案

#1


0  

county is 2 level deep.. zip and city are only one level deep. Thats the reason. Try this

县是2级深..拉链和城市只有一层深。这就是原因。尝试这个

target2.options[0] = new Option("Select Zipcode", "null");
for (var i = 0; i < xmlZips.length; i++) {target2.options[target2.options.length] = new Option(xmlZips[i].childNodes[0].nodeValue, xmlZips[i].childNodes[0].nodeValue, false, (matched == xmlZips[i].childNodes[0].nodeValue));
}

#1


0  

county is 2 level deep.. zip and city are only one level deep. Thats the reason. Try this

县是2级深..拉链和城市只有一层深。这就是原因。尝试这个

target2.options[0] = new Option("Select Zipcode", "null");
for (var i = 0; i < xmlZips.length; i++) {target2.options[target2.options.length] = new Option(xmlZips[i].childNodes[0].nodeValue, xmlZips[i].childNodes[0].nodeValue, false, (matched == xmlZips[i].childNodes[0].nodeValue));
}