[英]How to create a Javascript array from xml file attributes in 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
下的屬性,例如: mount
, free
, total
, used
和percentage
屬性
更新
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);
});
使用jQuery XSLT插件將xml轉換為正確的格式。
XML --> [XSLT Transformation] --> JS
源(XML)和輸出(JS)之間的這一層使您可以自由地維護,更改和擴展源和輸出,而無需創建意大利面。
可以在此處找到XSLT到JSON教程
編輯:示例實現看起來像
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output method="text" />
<xsl:template match="/">
<xsl:apply-templates select="csmclient/system/filesystem" />
</xsl:template>
<xsl:template match="filesystem">
<xsl:text>[</xsl:text>
<xsl:apply-templates select="file" />
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template match="file">
<xsl:text>{</xsl:text>
<xsl:apply-templates select="@*" />
<xsl:text>}</xsl:text>
<xsl:if test="position() < last()">,</xsl:if>
</xsl:template>
<xsl:template match="file/@*">
<xsl:value-of select="concat('"', name(), '":')" />
<xsl:choose>
<xsl:when test="string(number(.)) = 'NaN'">
<xsl:value-of select="concat('"',. , '"')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
<xsl:if test="position() < last()">,</xsl:if>
</xsl:template>
</xsl:stylesheet>
var data = [];
$(xml).find('file').each(function(){
var row = {};
row.mount = this.getAttribute("mount");
// ...
data.push(row);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.