【问题标题】:Javascript: Switch from importing data from XML file to importing data from a JS object arrayJavascript:从从 XML 文件导入数据切换到从 JS 对象数组导入数据
【发布时间】:2022-01-12 17:04:55
【问题描述】:

我需要编辑一个脚本来从从 xml 文件读取 XML 数据切换到从 JS 对象数组读取数据。

假设xml文件是x.xml:

<xml>
   <location>
     <name>cafe 1</name>
     <address>1 cafe st</address>
   </location>
   <location>
     <name>cafe 2</name>
     <address>2 cafe st</address>
   </location>
</xml>

以下代码使用 xml 文件中的数据填充数组

$.ajax({
               type: "GET",
               url: "x.xml",
               dataType: "xml",
               success: function(xml) {    
                   $(xml).find('location').each(function(){
   i +=1;
                       var name = $(this).find('name').text();
                       var address = $(this).find('address').text();
                     
                        table[i] = {name:name, address:address};
                       
                               
                   });

..我可以将输出重写为


var table = [
                  {"name":"cafe 1", "address":"1 cafe st"},
                  {"name":"cafe 2", "address":"2 cafe st"},
                  ]

...并使用

调用数组中的数据
var m; 
for ( m = 1; m < table.length-1; m++) {

                      if (table[m].name == "cafe 1" ....

试图让它尽可能接近这种语法。我只想以显示的格式将值放入数组中

$(xml).find('location').each(function() {
    i += 1;
    var name = $(this).find('name').text();
    var address = $(this).find('address').text();
    
  table[i] = {name:name, address:address};
  });

【问题讨论】:

    标签: javascript arrays xml


    【解决方案1】:

    你基本上有它,虽然我在这里简化了你的代码

    let xml = `<xml>
       <location>
         <name>cafe 1</name>
         <address>1 cafe st</address>
       </location>
       <location>
         <name>cafe 2</name>
         <address>2 cafe st</address>
       </location>
    </xml>`
    
    let table = []
    $(xml).find('location').each(function(i) {
      // here's one way, using array.push()
      /* 
      table.push({
        name: $(this).find('name').text(),
        address: $(this).find('address').text()
      });
      */
      
      // heres another way, using a specific index
      // i (the index) is supplied by the $.each function
      table[i] = {
        name: $(this).find('name').text(),
        address: $(this).find('address').text()
      }
    });
    
    table.forEach(row => {
    $('#theTable').append(`<tr><td>${row.name}</td><td>${row.address}</td></tr>`)
    })
    #theTable td {
    padding:5px;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <table id='theTable' border='1'><tr><td>Name</td><td>Address</td></tr></table>

    【讨论】:

    • 非常感谢!有没有办法将 url: "x.xml" 更改为不同的参数以获取原始 xml?非常感激。我正在尝试尽可能少地更改现有脚本,只需将数据源从 xml 文件更改为页面 xml/array。
    • 如何编辑 table.push 部分以将每个值设置为数组,例如 table[i] = {name:name, address:address}; 我现在只需要将值从 xml 放入数组中,谢谢。
    • 对不起,我不关注这些 cmets 中的任何一个。第一个 - 您正在使用 ajax 从远程源获取数据......您是在问如何只使用页面中已经存在的数据而不是 ajax?第二个 - table.push() 正在获取一个数组 table 并向其添加索引,所以在我的回答中,table[1] 是表数组的第二个索引并且具有以下值:{name: 'cafe 2', address: '2 cafe st'}。不确定这是否有助于清除它......
    • 感谢您对我的包容。是的,我正在从这个第 3 方脚本中删除 ajax,并且我想处理页面上的数据。我特别需要为您的代码设置一个增量器 i+=1,以便它填充像 table[i] = {name:name, address:address}; 这样的数组我更新了问题的结尾
    • 好的,我的示例使用页面上的数据而不是 ajax - 那里需要更改什么?对于您的第二点,我更新了我的答案....
    猜你喜欢
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    相关资源
    最近更新 更多