XML getElementsByTagName在中途退出

时间:2013-03-12 09:08:10

标签: javascript xml

我有一个XML文件如下:

<products>
<handle id = "B1-12">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin Del Mar Handle in Venetian Bronze</full_name>
    <partial_name>Baldwin Del Mar</partial_name>
</handle>
<handle id = "B2-12">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin Elizabeth Handle in Venetian Bronze</full_name>
    <partial_name>Baldwin Elizabeth</partial_name>
</handle>
<handle id = "B3-12">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin New Hampshire Handle in Venetian Bronze</full_name>
    <partial_name>Baldwin New Hampshire</partial_name>
</handle>
<handle id = "B4-12">
    <single_price>230</single_price>
    <double_price>460</double_price>
    <full_name>Baldwin Westcliff Handle in Venetian Bronze</full_name>
    <partial_name>Baldwin Westcliff</partial_name>
</handle>
<handle id = "B1-49">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin Del Mar Handle in Matte Brass & Black</full_name>
    <partial_name>Baldwin Del Mar</partial_name>
</handle>
<handle id = "B2-49">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin Elizabeth Handle in Matte Brass & Black</full_name>
    <partial_name>Baldwin Elizabeth</partial_name>
</handle>
<handle id = "B3-49">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin New Hampshire Handle in Matte Brass & Black</full_name>
    <partial_name>Baldwin New Hampshire</partial_name>
</handle>
<handle id = "B4-49">
    <single_price>230</single_price>
    <double_price>460</double_price>
    <full_name>Baldwin Westcliff Handle in Matte Brass & Black</full_name>
    <partial_name>Baldwin Westcliff</partial_name>
</handle>
<handle id = "B1-52">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin Del Mar Handle in Matte Antique Nickel</full_name>
    <partial_name>Baldwin Del Mar</partial_name>
</handle>
<handle id = "B2-52">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin Elizabeth Handle in Matte Antique Nickel</full_name>
    <partial_name>Baldwin Elizabeth</partial_name>
</handle>
<handle id = "B3-52">
    <single_price>265</single_price>
    <double_price>530</double_price>
    <full_name>Baldwin New Hampshire Handle in Matte Antique Nickel</full_name>
    <partial_name>Baldwin New Hampshire</partial_name>
</handle>
<handle id = "B4-52">
    <single_price>230</single_price>
    <double_price>460</double_price>
    <full_name>Baldwin Westcliff Handle in Matte Antique Nickel</full_name>
    <partial_name>Baldwin Westcliff</partial_name>
</handle>
<handle id = "P1">
    <single_price>350</single_price>
    <double_price>600</double_price>
    <full_name>Pull Handle 1</full_name>
    <partial_name>Pull Handle 1</partial_name>
</handle>
<handle id = "P3">
    <single_price>350</single_price>
    <double_price>600</double_price>
    <full_name>Pull Handle 2</full_name>
    <partial_name>Pull Handle 2</partial_name>
</handle>
<handle id = "P4">
    <single_price>350</single_price>
    <double_price>600</double_price>
    <full_name>Pull Handle 3</full_name>
    <partial_name>Pull Handle 3</partial_name>
</handle>
</products>

我正在尝试获取句柄项以便稍后使用这些数据。但是,第五项似乎失败了。当我这样做时:

var xml = new XMLHttpRequest();

xml.onreadystatechange = function ()
{
   if(xml.readyState == 4)
   {
      var xmlDoc = xml.responseXML;
      if(!xmlDoc)
      xmlDoc = (new DOMParser()).parseFromString(xml.responseText, 'text/xml');

      var handle_list = xmlDoc.getElementsByTagName("handle");

      console.log(handle_list);
   }
}

我在控制台中收到了这个:

[handle, handle, handle, handle, handle, item: function]
0: handle
1: handle
2: handle
3: handle
4: handle
length: 5
__proto__: NodeList

此外,日志中索引4处的项目不完整。我在这里做错了什么?

1 个答案:

答案 0 :(得分:1)

我正在添加我的评论作为答案,因为它似乎是您可以将此问题标记为已回答的答案。

  

标签周围的xml文件中有一个未转义的&符号   解析器停止。也许这是你问题的原因?