【问题标题】:Pulling second column only from XML into array仅将第二列从 XML 拉入数组
【发布时间】:2015-12-09 11:49:09
【问题描述】:

我正在尝试拉入一个仅包含 2 列但多行的外部文件,第一个只是升序的数字引用,应该被跳过,但第二个包含我需要推入数组的值所以它们可以被注入到页面中。到目前为止,我所能做的就是拉入所有值。

var catalogIn = [];

$.ajax({
    url: 'content-catalog.xml',
    type: 'GET',
    dataType: 'xml',
    success: function (returnedXMLResponse) {
        $(returnedXMLResponse).find('Row').each(function () {
            // loop over each cell
            $('Data', this).each(function () {
                // push row into main array
                catalogIn.push($(this).text());
            });
        })
    }
});

【问题讨论】:

标签: jquery arrays xml


【解决方案1】:

首先你需要像这样解析xml响应

var xmlresponse = $.parseXML(returnedXMLResponse);

然后通过循环xmlresponse找到一个节点。

var $xml = $(xmlresponse);
var $data = $xml.find("<your node>");

$data.each(function(){

    var column1= $(this).find('your column 1').text(),
     var column2= $(this).find('your column 2').text(); 
});

【讨论】:

    【解决方案2】:

    为什么不检查列索引然后决定是否应该将元素添加到数组中

    var catalogIn = [];
    
    $.ajax({
        url: 'content-catalog.xml',
        type: 'GET',
        dataType: 'xml',
        success: function (returnedXMLResponse) {
            $(returnedXMLResponse).find('Row').each(function () {
                // loop over each cell
                $('Data', this).each(function (index) {
                    if(index == 1) {
                    // push row into main array
                    catalogIn.push($(this).text());
                    }
                });
            })
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2019-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      相关资源
      最近更新 更多