javascript - 使用 Jquery 解析 XML 数组

标签 javascript jquery xml jquery-mobile

我遇到了使用 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()获得它的值(value)。

$(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>

关于javascript - 使用 Jquery 解析 XML 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30972347/

相关文章:

javascript - 如何检查二叉树的右侧。在树中搜索项目

javascript - 如何根据 jquery 中的 url 状态加载 div

javascript - jquery 成功时赋值

javascript - jqGrid无法从服务器加载xml数据

xml - 如何获取 XML 文档中的当前位置

javascript - 排序 HTML ul

javascript - 获取json,图片url

5.2 中带引号转义的 PHP Json 编码?

javascript - 动态更改可放置元素的 CSS

java - XML 与 JSON 将完整的树传递到 UI 上基于决策的动态表单