【问题标题】:Create array from xml nodes using jquery使用 jquery 从 xml 节点创建数组
【发布时间】:2015-11-22 19:20:22
【问题描述】:

我正在尝试将 xml 文件中的纬度/经度点隔离到数组中。我正在将 lat/lon 子节点文本推送到数组中。理想情况下,我希望数组读取为:

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

这是我目前所拥有的:

 $.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:

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

如何将每个点的纬度/经度对推送到数组中?

【问题讨论】:

    标签: jquery arrays ajax xml


    【解决方案1】:

    也许您可以创建一个对象point,然后为xml 中的每个点创建一个属性“point[n]”,并使用counter 在其上附加一个递增的数字。然后第一个属性的名称将变为“point1”。

    然后将具有“lat”和“lon”值的数组添加到该属性。

    这样您就可以像这样在数组中获取“纬度”和“经度”值:

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

    例如:

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

    console.log 语句将导致:

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

    【讨论】:

      猜你喜欢
      • 2016-05-13
      • 1970-01-01
      • 2021-10-02
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多