How to pull data from XML files
How to pull data from XML files
First of all, congratulations on such a wonderful tool. But i would really like to suggest that you people should work more on the documentation part for newbies like me.
I am a beginner in programming and have to create a presentation CD with tabular data and for that i chose this datatables. I have the data in excel files which i previously thought of converting into xml and parse through javascript. Due to limited time i had to use some prebuild code like datatables but i just cant configure it.
First of all, i am trying to store all the XML data in a 2d array like this
[code]
function import_xml()
{
var x = [];
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","author.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
data=xmlDoc.getElementsByTagName("paper");
for(i=0 , l = data.length; i < l ; i++)
x.push(data[i]);
[/code]
Now i have all the tabular data in the array x. I just cannot figure out how to use it with datatables initializing code. I have tried the following code. ( 5 colums in my table --> GID, TID, PID, Title and Authors)
[code]
$(document).ready(function() {
$('#author_data').html( '' );
$('#author_data').dataTable( {
"aaData": x,
"aoColumns": [
{ "sTitle": "GID" },
{ "sTitle": "TID" },
{ "sTitle": "PID" },
{ "sTitle": "Title" },
{ "sTitle": "Authors", "sClass": "center" },
{
"fnRender": function(obj) {
var sReturn = obj.aData[ obj.iDataColumn ];
if ( sReturn == "A" ) {
sReturn = "A";
}
return sReturn;
}
}
]
} );
} );
[/code]
All i can see is a plane table with 2 rows of dummy data which i included in my html page in the table with id="author_data", as instructed in the blogs. I have no knowledge of jquery, so please try to explain in easy terms....
I am a beginner in programming and have to create a presentation CD with tabular data and for that i chose this datatables. I have the data in excel files which i previously thought of converting into xml and parse through javascript. Due to limited time i had to use some prebuild code like datatables but i just cant configure it.
First of all, i am trying to store all the XML data in a 2d array like this
[code]
function import_xml()
{
var x = [];
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","author.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;
data=xmlDoc.getElementsByTagName("paper");
for(i=0 , l = data.length; i < l ; i++)
x.push(data[i]);
[/code]
Now i have all the tabular data in the array x. I just cannot figure out how to use it with datatables initializing code. I have tried the following code. ( 5 colums in my table --> GID, TID, PID, Title and Authors)
[code]
$(document).ready(function() {
$('#author_data').html( '' );
$('#author_data').dataTable( {
"aaData": x,
"aoColumns": [
{ "sTitle": "GID" },
{ "sTitle": "TID" },
{ "sTitle": "PID" },
{ "sTitle": "Title" },
{ "sTitle": "Authors", "sClass": "center" },
{
"fnRender": function(obj) {
var sReturn = obj.aData[ obj.iDataColumn ];
if ( sReturn == "A" ) {
sReturn = "A";
}
return sReturn;
}
}
]
} );
} );
[/code]
All i can see is a plane table with 2 rows of dummy data which i included in my html page in the table with id="author_data", as instructed in the blogs. I have no knowledge of jquery, so please try to explain in easy terms....
This discussion has been closed.
Replies
The DataTables project is just me, so I'm afraid I only have a limited amount of time in which I can develop DataTables, write documentation and provide support while also trying to earn a living :-). This is one of the reasons why donations and support for DataTables are so great fully received as they allow the project to progress.
One of the areas where feedback is very welcome indeed is what kind of documentation would be helpful for people such as yourself. Obviously I can't teach Javascript from scratch so I have to assume a certain leave of understanding of Javascript if you want to do anything more than a simple table, but any input on this would be welcome not only by myself and also anyone else how would then benefit in turn from improved documentation.
At the end of your import_xml() function what is 'x' - it looks to me like it would be a 1D array (depending on what data[i] is). If you add console.dir( x ); you will be able to see the output on your browser's console. DataTables is expecting a 2D array for aaData - like in this example: http://datatables.net/release-datatables/examples/data_sources/js_array.html .
A link to your page would also be very useful to allow me to see and debug it live.
Allan
And about the code, nothing is online, and i guess it not going to be. Like i said earlier, i need to create an autorun CD with some sort of representation for the data i have. I thought of using it with javascript and all and came here looking for solutions. I am not sure if it is the right way to do this.
The data[i] in the above code is an element from the DOM tree i created using getElementByTagName. Since DOM node trees are not editables, i copied them all in array named 'X'. I am trying to use that with datatables. This is where i am stuck.
The syntax you gave me to check for the type of x in console is not working. I created a script and printed all the data in a simple table through this x and it is working just fine. I dont know what the problem is now.
[code]
x = [
[ 1, 2, 3 ],
[ 4, 5, 6 ]
];
[/code]
What you could do is add:
[code]
console.log( JSON.stringify( x ) );
[/code]
at the end of the import_xml() function and then paste the result in to this thread so we can see exactly what is happening.
Thanks,
Allan