简体   繁体   English

使用jQuery从XML节点创建数组

[英]Create array from xml nodes using jquery

I'm trying to isolate lat/lon points in xml file to array. 我正在尝试将xml文件中的经/纬度点隔离到数组中。 I am getting hung up on pushing the lat/lon child nodes text to the array. 我不愿意将lat / lon子节点文本推送到数组。 Ideally I would like the array to read as: 理想情况下,我希望数组读为:

point 1 = lat1, lon1 点1 = lat1,lon1
point 2 = lat2, lon2 点2 = lat2,lon2

Here's what I have so far: 这是我到目前为止的内容:

 $.ajax({
        type: "GET",
        url: "points.xml",
        dataType: "xml",
        success: function (xml) {
            //find each TP
            var areaPlots = $(xml).find('plot_area').each(function () {                     $(this).find('area').each(function () {
                    var areas = $(this);
                    $(areas).find('point').each(function () {
                        var points = $(this);
                        $(points).find('lat_num').each(function () {
                          var latitude = $(this).text();    
                        $(points).find('lon_num').each(function () {
                          var longitude = $(this).text();                                                                                                           
                        })
                    })
                })
            })      
             //end of success function
        }

XML: XML:

<plot_area>
 <area>  
  <point>
   <lat_num>42.00861</lat_num> 
   <lon_num>-94.00351</lon_num> 
 </point>
 <point>
  <lat_num>43.00921</lat_num> 
  <lon_num>-89.00024</lon_num> 
 </point>
 <point>
  <lat_num>02.008427</lat_num> 
  <lon_num>-74.99588</lon_num> 
 </point>
 </area>
</plot_area>

How do I push each point's lat/lon pair to an array? 如何将每个点的经/纬对推入数组?

Maybe you can create an object point and then for each point the xml, create a property 'point[n]' with an incrementing number appended to it using a counter . 也许您可以创建一个对象point ,然后为xml的每个点创建一个属性'point [n]',并使用counter为其添加一个递增数字。 The name of the first property would then become 'point1'. 然后,第一个属性的名称将变为“ point1”。

Then add an array with the 'lat' and 'lon' values to that property. 然后将具有“ lat”和“ lon”值的数组添加到该属性。

That way you can obtain your 'lat' and 'lon' values in an array like this: 这样,您可以在像这样的数组中获取“ lat”和“ lon”值:

point.point1;
point.point2;
point.point3;

For example: 例如:

<script type="text/javascript">
    $.ajax({
        type: "GET",
        url: "points.xml",
        dataType: "xml",
        success: function (xml) {

            var point = {};
            var counter = 1;

            //find each TP
            var areaPlots = $(xml).find('plot_area').each(function () {

                $(this).find('area').each(function () {
                    var areas = $(this);
                    $(areas).find('point').each(function () {
                        var points = $(this);
                        var name = "point" + counter;
                        point[name] = [];

                        $(points).find('lat_num').each(function () {
                            point[name].push($(this).text());
                            $(points).find('lon_num').each(function () {
                                point[name].push($(this).text());
                            });
                        });
                        counter++;
                    });
                    console.log(point.point1);
                    console.log(point.point2);
                    console.log(point.point3);
                });
                //end of success function
            });
        }
    });
</script>

The console.log statements will result in: console.log语句将导致:

["42.00861", "-94.00351"]
["43.00921", "-89.00024"]
["02.008427", "-74.99588"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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