AabinGunz
AabinGunz

Reputation: 12347

What or which plugin in jquery shall i use to populate a html table with a xml file content?

I have a requirement to display data from a xml file from server (path to file something like files/client.xml) into a html table or datagrid, which plugin or rather what should i use so that it has variable pagination, filter and table css customization. Any suggestions would help, a little example should be a plus point for me :) Thanks

Note: My xml structure is fixed

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

Updating with working solution

Since it does not take attribute.. like here i'd like to get mount,free etc Here is what i did in jqGrid for the above xml.

var i=0;
var filesystem=[];
$(xml).find('file').each(function(){ 
    var row={};
    row.id=i++;
    row.total=$(this).attr('total');
    row.free=$(this).attr('free');
    row.used=$(this).attr('used');
    row.percentage=$(this).attr('percentage');
    filesystem.push(row);
});


$('#detailTable').empty();
$('<div width="100%">')
.attr('id','diskUsageSpan')
.html('<div class="titleBlue">Configuration&gt;System&gt;Disk Usage</div>'+
        '<table id="list1" width="100%"></table>'+
        '<div id="gridpager"></div>'+
    '</div>')       
.appendTo('#detailTable');  



jQuery("#list1").jqGrid({
    datatype: "clientSide",
    height: 250,
    colNames:['id','Total Space','Free Space', 'Used Space', 'Used Percentage'],
    colModel:[
        {name:'id',index:'id', width:90, align:"right"},
        {name:'total',index:'total', width:90, align:"right"},
        {name:'free',index:'free', width:90, align:"right"},
        {name:'used',index:'used', width:90, align:"right"},
        {name:'percentage',index:'percentage', width:120, align:"right"}
    ],
    pagination:true,
    pager : '#gridpager',
    rowNum:10,
    scrollOffset:0,
    height: 'auto',
    autowidth:true,
    viewrecords: true,
    gridview: true,
    edit:false,
    add:false,
    del:false

});



for(var i=0;i<filesystem.length;i++)
    jQuery("#list1").jqGrid('addRowData',i+1,filesystem[i]);

jQuery("#list1").setGridParam({rowNum:10}).trigger("reloadGrid");

Works perfectly Thanks @Tomas and @doctrey

Upvotes: 0

Views: 380

Answers (3)

Tomas Aschan
Tomas Aschan

Reputation: 60614

Basically, you can read the XML DOM just as you read the HTML DOM, using jQuery selectors. So in your XML example, if you want to do something specific with each <file> element - say, add the contents of it's mount attribute to an unordered list, you could do something like this:

$(xml).('file').children().each(function() {
    var fileElem = this; // save the instance for closure
    $('ul#theList').append($('<li>').text(fileElem.attr('mount'));
});

You can get the XML contents with AJAX, using jQuery's built-in AJAX API:

$.ajax({
    type: "GET",
    url: "your.xml",
    dataType: "xml",
    success: function(xml) {
        // Insert the previous code snippet here
    }
});

I got all of this from this tutorial, so it might be helpful for you too. Note: This was the very first hit on Google for "jquery xml"...

Upvotes: 1

hughes
hughes

Reputation: 5713

You probably want to use XSLT - it will let you take the XML response and directly apply an XSL transform, which can then be inserted into the DOM.

Upvotes: 0

David Wick
David Wick

Reputation: 7105

jquery.dataTables is good as long as you have control over the contents of the xml file. Their formatting requirements are rather strict.

Upvotes: 0

Related Questions