javascript - 如何从 jQuery 中的 xml 文件属性创建 Javascript 数组

标签 javascript jquery

我正在使用 jqGrid , 但它不能从 xml 文件中获取属性。所以我想在 xml 中创建一个数组,如下所示。请帮我从下面的 xml 文件创建一个数组

jqGrid支持的数组格式

var mydata = [
    {id:"1",invdate:"2007-10-01",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"2",invdate:"2007-10-02",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"3",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"4",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"5",invdate:"2007-10-05",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"6",invdate:"2007-09-06",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"},
    {id:"7",invdate:"2007-10-04",name:"test",note:"note",amount:"200.00",tax:"10.00",total:"210.00"},
    {id:"8",invdate:"2007-10-03",name:"test2",note:"note2",amount:"300.00",tax:"20.00",total:"320.00"},
    {id:"9",invdate:"2007-09-01",name:"test3",note:"note3",amount:"400.00",tax:"30.00",total:"430.00"}
    ];

我的xml

<?xml-stylesheet type="text/xsl" href="csmclientiir.xsl"?>
<csmclient product="abc"   date="4/26/11 2:05 PM">
<system>
<osname>Linux
</osname>
<hostname>AbhishekNix
</hostname>
<release>2.6.18-128.el5
</release>
<filesystem>
    <file mount='/home/hp1' home='(innfs2:/vol/home/shome/home/hp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    <file mount='/home/par21' home='(innfs2:/vol/home/shome/home/par21)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    <file mount='/home/h231' home='(innfs2:/vol/home/shome/home/h231)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    <file mount='/home/avallin1' home='(innfs2:/vol/home/shome/home/avallin1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    <file mount='/home/park' home='(innfs2:/vol/home/shome/home/park)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    <file mount='/home/sp1' home='(innfs2:/vol/home/shome/home/sp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    <file mount='/home/ganga1' home='(innfs2:/vol/home/shome/home/ganga1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
    <file mount='/home/nbp1' home='(innfs2:/vol/home/shome/home/nbp1)' total='1717567488' free='644306780' used='1073260708' percentage='62' />
</filesystem>
</system>
<product>
<showtime>Tue Apr 26 14:05:23 2011
</showtime>
</product>
</csmclient>

如何在 jquery 或 JS 中创建一个 JS 数组,从上面的 xml 中获取 csmclient>system>filesystem>file 下的属性 eg: mount, freetotalusedpercentage 属性

已更新

var filesystem=[];
$(xml).find('file').each(function(){ 
    console.info($(this).attr('total')+", "+$(this).attr('free')+", "+$(this).attr('used')+", "+$(this).attr('percentage'));
    var row={};
    row.total=$(this).attr('total');
    row.free=$(this).attr('free');
    row.used=$(this).attr('used');
    row.percentage=$(this).attr('percentage');
    filesystem.push(row);
});

最佳答案

var data = [];

$(xml).find('file').each(function(){ 
  var row = {};
  row.mount = this.getAttribute("mount");
  // ...
  data.push(row);
});

关于javascript - 如何从 jQuery 中的 xml 文件属性创建 Javascript 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6394498/

相关文章:

javascript - 如何编写接受回调作为参数的 jquery 函数

javascript - 单击按钮删除 div(更改 id)

javascript - 为什么我使用 setTimeout 的同步代码在 JavaScript 中表现得异步?

javascript - anchor 包装器上的 jQuery .one ("click") 会导致 href 不被遵循

javascript - 我的 javascript 代码中的字符串比较有什么问题?

javascript - 将 CSV 解析为哈希数组

javascript - Bootstrap 日期选择器

Jquery图像加载顺序

javascript - 如何添加日期验证?

jquery - 为什么第二次调用 .html() 会阻止 click() 函数工作?