如何使用jquery

时间:2018-07-16 19:10:15

标签: javascript jquery xml xml-parsing

我有这样的XML

<Response>
  <Title>
    <RequestID>1</RequestID>
  </Title>
  <RequestID>120</RequestID>
</Response>

我只需要获取RequestID值120。

var xml = "<Response><Title><RequestID>1</RequestID></Title><RequestID>120</RequestID></Response>";
var xmlData = $.parseXML(xml);
var RID = $xmlData.find('RequestID');
//It is returning the data like [1, 120];

如何才能从RequestID的父节点(不是嵌套的子节点)获取值?

2 个答案:

答案 0 :(得分:1)

您可以使用child selector。子选择器仅选择第一级后代。

var xml = "<Response><Title><RequestID>1</RequestID></Title><RequestID>120</RequestID></Response>";
var xmlData = $.parseXML(xml);
var RID = $(xmlData).find('Response > RequestID').text();
console.log(RID);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:-1)

尝试:

var xml = "<Response><Title><RequestID>1</RequestID></Title><RequestID>120</RequestID> 
</Response>";
var $xmlData = $.parseXML(xml);
var RID = $($xmlData).find('RequestID');
console.log(RID);