jquery迭代的xml响应

时间:2015-08-31 09:31:24

标签: javascript jquery ajax xml

我有来自ajax调用的xml respnse。我想迭代它。 下面是我在SoaUi的xml输出。

我想要输出响应如下:

50 Water St Oakland USA
101 Emerall New York USA
Sea Point CA USA

请帮我在jQuery中写回复。

修改: 这是我在xml中的回复:

<ns3:Row>
 <ns3:AddressLine1>50 Water St</ns3:AddressLine1> 
 <ns3:City>Oakland</ns3:City>
 <ns3:Country>USA</ns3:Country>
</ns3:Row>
<ns3:Row>
 <ns3:AddressLine1>101 Emerall</ns3:AddressLine1>
 <ns3:City>New York</ns3:City>
 <ns3:Country>USA</ns3:Country>
</ns3:Row>
<ns3:Row> 
 <ns3:AddressLine1>Sea Point</ns3:AddressLine1>
 <ns3:City>CA</ns3:City>
 <ns3:Country>USA</ns3:Country> 
</ns3:Row>

2 个答案:

答案 0 :(得分:0)

您可以获取循环遍历元素所需的信息,并将信息存储在字符串或您认为合适的任何内容中。

一个解决方案(混合jquery和普通javascript)可以如下:

$(f).each(function (ind, el) {
    var line = '';
    $(el).children().each(function (ind, nod) {
        line += nod.childNodes[0].nodeValue + ' ';
    });
    console.log(line.slice(0, -1));
});

变量&#39; f&#39;包含从服务器检索的XML字符串。

您可以在此fiddle中对其进行测试(打开开发人员的控制台以查看其中打印的结果)。

希望它有所帮助。

答案 1 :(得分:0)

解决方案就在这里。

var geoCompAddress;


$(data).find('ns3\\:Row').each(function() {

geoCompAddress = $(this).find('ns3\\:AddressLine1').text()+ ',' + $(this).find('ns3\\:City').text()+ ',' + $(this).find('ns3\\:Country').text() ;

});