简体   繁体   中英

Parsing XML using Javascript

I want to access XML elements in the following document. There about 935 items AircraftReport. I know how to display all of them by using .find() and .each() but I am having trouble getting only the first 10 AircraftReports.

<response>
<data num_results="935">
<AircraftReport>
     <latitude>50.5</latitude>
     <longitude>-51.8</longitude>
</AircraftReport>
<AircraftReport>
     <latitude>55.9167</latitude>
     <longitude>-43.1</longitude>
</AircraftReport>
....
</data>
</response>

I am assuming I have to write that in a for loop. How can this be done with jQuery?

There are 2 ways to solve this problem... the first is to enhance the server side code to support paging where by the server will send only 10 items per request with corresponding paging details...

Another is to do it in the client side using slice()

$(this).find("AircraftReport").slice(0, 10).each(function(){...} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM