I'm now working with API calls currectly, so most the requests are done through ajax call. As you can expect, both the data sent to server and data received from server will be xml data. I've successfully able to dynamically construct the xml data during the sign up process and during post requests. By utilising JaxB, I've done the necessary steps in server.
Now I've to get the data as xml from server if a user needs to view a ceertain resource. Like before, I'm using jaxb to convert Java object into xml data and I'm getting the xml data in success function of Javascript. There are lot of examples and questions present on converting this xml data into user viewable form.
By my main goal is to make every function Dynamic, Consider now I'm going to show the list of users to admin, I can use these examples to convert my xml data into tables.
Display JSON Data in HTML Table
populate jquery data table with returned json data
If I'm doing like that, I've to manually write the same process with some modification based on table fields for every list view. This is a bad practise if I'm going to copy paste the same code with some modifications for 10 list views. I would like to make the xml to table conversion as a common function for any number of tables no matter how many fields are present in it.
I'm getting the xml data from server as String. So I've converted it to recognizable xmlData using following code.
var xmlData = jQuery.parseXML(data); //data is the xml String which I'm getting from server
//Converting xmlData into JSON Objects
var containerTag = myTag //I can get this from - document.getElementById("tableId").name
var output = new Array( );
var rawData = xmlData.getElementsByTagName(containerTag)[0];
var i, j, oneRecord, oneObject;
for (i = 0; i < rawData.childNodes.length; i++) {
if (rawData.childNodes[i].nodeType == 1) {
oneRecord = rawData.childNodes[i];
oneObject = output[output.length] = new Object( );
for (j = 0; j < oneRecord.childNodes.length; j++) {
if (oneRecord.childNodes[j].nodeType == 1) {
oneObject[oneRecord.childNodes[j].tagName] = oneRecord.childNodes[j].firstChild.nodeValue;
}
}
}
}
By displaying the data as console.log(output[0]);
, I'm getting my real data. But I searched to use this information to populate it in table, most of them suggests to do it like
.fieldname1
.fieldname2
and so on which is not I'm expecting to. I've been learning Javascript now a days, but I dont know how to make the process common for all tables irrespective of no of fileds.
Note: I'm using jquery datatables.
Just a thought comes up in my head. Is it possible to just give the Json object to jquery datatables and it'll do the remaining process..?
For reference, this is my xml data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Users>
<user>
<id>1</id>
<username>user1</username>
<email>email1</email>
<status>active</status>
</user>
<user>
<id>2</id>
<username>user2</username>
<email>email2</email>
<status>active</status>
</user>
<user>
<id>3</id>
<username>user3</username>
<email>email3</email>
<status>inactive</status>
</user>
</Users>
This is the Json object
Object { id: "1", username: "user1", email: "email1", status: "active" } //output[0]
Object { id: "2", username: "user2", email: "email2", status: "active" } //output[1]
Object { id: "3", username: "user3", email: "email3", status: "inactive" } //output[2]