Jquery:从xml中获取确切的值

时间:2015-02-17 13:00:31

标签: javascript php jquery xml

我下面的xml,到达日期时间是xml的两倍,但我需要第一个到达日期时间,

<segment>
      <index>1</index>
      <departure-airport>MAA</departure-airport>
      <arrival-airport>IXB</arrival-airport>
      <departure-date-time>2015-03-17T07:55:00</departure-date-time>
      <arrival-date-time>2015-03-17T11:45:00</arrival-date-time>
      <flight-number>503</flight-number>
      <airline>6E</airline>
      <operating-airline>6E</operating-airline>
      <stops>1</stops>
      <equipment>320 </equipment>
      <duration>13800</duration>
      <stops-details>
          <stop-details>
              <layover-airport>CCU</layover-airport>
              <arrival-date-time>2015-03-17T10:10:00</arrival-date-time>
              <departure-date-time>2015-03-17T10:40:00</departure-date-time>
              <layover-duration>1800</layover-duration>
          </stop-details>
      </stops-details>
    </segment>

我试过这样,但没有工作。任何帮助表示赞赏!!!!

var arr_tym =$(element).find('departure-date-time').text();

这对我有用,但在IE8中它不起作用

var arr_tym =$(element).find('arrival-date-time')[0].innerHTML

1 个答案:

答案 0 :(得分:1)

尝试使用:first仅选择第一个

&#13;
&#13;
var xml = '<segment> \
      <index>1</index> \
      <departure-airport>MAA</departure-airport> \
      <arrival-airport>IXB</arrival-airport> \
      <departure-date-time>2015-03-17T07:55:00</departure-date-time> \
      <arrival-date-time>2015-03-17T11:45:00</arrival-date-time> \
      <flight-number>503</flight-number> \
      <airline>6E</airline> \
      <operating-airline>6E</operating-airline> \
      <stops>1</stops> \
      <equipment>320 </equipment> \
      <duration>13800</duration> \
      <stops-details> \
          <stop-details> \
              <layover-airport>CCU</layover-airport> \
              <arrival-date-time>2015-03-17T10:10:00</arrival-date-time> \
              <departure-date-time>2015-03-17T10:40:00</departure-date-time> \
              <layover-duration>1800</layover-duration> \
          </stop-details> \
      </stops-details> \
    </segment>';

var arr_tym =$(xml).find('departure-date-time:first').text();
console.log(arr_tym);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;