0

我有变量response,持有一个 XML 字符串

<tag>
  <innerTag>
    <ex0>1000</ex0>
    <ex1>2000</ex1>
    <ex2>3000</ex2>
  </innterTag>
  <innerTag>
    <ex0>4000</ex0>
    <ex1>7000</ex1>
    <ex2>2500</ex2>
  </innterTag>
</tag>

我想解析字符串并将每个标签值添加到我可以用来进一步处理数据的变量中。我正在使用 jQuery,到目前为止我得到了

[...]

var response = request.responseXML.documentElement;
$.get(response, {}, function(xml){
  $('innerTag', xml).each(function(){
    ex0 = $(this).find("ex0").text();
    ex1 = $(this).find("ex1").text()
    ex2 = $(this).find("ex2").text()    
  })
})

[...]

最明显的问题是我有多个“innerTag”。如何将每个值分配给变量?
问题是,我需要将所有 ex0、ex1 和 ex2 传递给另一个函数,因此明智的做法是将变量命名为 inner0_ex0、inner1_ex0 等不同的 ex0 值等。

4

1 回答 1

0

一种可能性是将结果存储到这样的数组中:

$.get(response, {}, function(xml){
  var ex0 = [];
  var ex1 = [];
  var ex2 = [];
  $('innerTag', xml).each(function(){
    ex0.push($(this).find("ex0").text());
    ex1.push($(this).find("ex1").text());
    ex2.push($(this).find("ex2").text());
  });
  /* Access them here as ex0[0] or ex0[1], etc. */
})
于 2013-06-28T11:51:07.563 回答