使用Jquery分析XML数组

Parsing XML array using Jquery

本文关键字:数组 XML 分析 Jquery 使用      更新时间:2023-09-26

我遇到了使用Jquery传递XML的问题。在遍历jquery时,我得到了空数组。请帮助我如何从XML数组中获取数据。我在下面提到了我的代码。

XML

<?xml version="1.0" encoding="UTF-8"?>
<json>
   <json>
     <CustomerName>999GIZA MID INSURANCEAND SERVICES PVT LTD</CustomerName>
      <mobiLastReceiptDate>null</mobiLastReceiptDate>
   </json>
   <json>
      <CustomerName>A  SHRIVENGATESH</CustomerName>
      <mobiLastReceiptDate>null</mobiLastReceiptDate>
   </json>
   <json>
      <CustomerName>A 1 PROCESS</CustomerName>
      <mobiLastReceiptDate>null</mobiLastReceiptDate>
   </json>
   <json>
      <CustomerName>A A A ENTERPRISES</CustomerName>
      <mobiLastReceiptDate>null</mobiLastReceiptDate>
   </json>
   <json>
      <CustomerName>A ALAGUSUNDARAM</CustomerName>
      <mobiLastReceiptDate>null</mobiLastReceiptDate>
   </json>
</json>

Jquery

page_response=getResponse("yyyURL");
page_response.success(function(data){
console.log(data.results[0]);
console.log($( data ).find( "CustomerName" ));
$(data).find("json").each(function(i, item) {
var heures = $(item).attr("CustomerName");
var nbr = $(item).attr("EMI");
<!--- Am getting array.. ineed to get name and EMI-->
console.log(heures);
});
});

EMICustomerNamejson下的元素,因此可以使用.find()查找这些元素,然后使用text()获取其值。

$(data).find("json").each(function (i, item) {
    var heures = $(item).find("CustomerName").text();
    var nbr = $(item).find("EMI").text();
    console.log(heures);
});

.attr()用于获取元素的属性值,如<json EMI="abc">...</json>

您使用了错误的方法来获取节点,xml元素的基础是"node has attributes"。因此,如果你想在XML元素中找到任何节点,你可以使用jquery.find(),但当你想在这种情况下找到某个节点的任何属性时,你可以用jquery.attr()函数。

此外,jquery.val()和jquery.text()是分别用于获取元素的值和文本的函数。

希望这会有所帮助!!

干杯!!