Search Web......

Generic XML Display Utility for JavaScript


Wiki Javascript, Wikipedia JavaScript, Generic XML Display Utility for JavaScript, XML processing Java Script, Generic XMl processing JavaScript, XML read from JavaScript, XML from JS

There are many a times when we require to display the XML at client side using JavaScript. Most of the time when we are interacting with the web services the response is an XML response. The utility I have created is useful to display a XML file in the form of table on browser.
This script can display as many columns as you want on the table. The table headers are populated based on the XML field names which are specified in Field tag <Field>FieldName</Field>


So if you are sending only first name and last name in your XML response then the XML DocStructure will look like this



<DocStructure>

<Field>FirstName</Field>

<Field>LastName</Field>

</DocStructure>


Here is the JavaScript file XMLGenericDisplay.js, which can be used for this.



//Global Variables

var xDoc;

/*

Change below global variable values to match your XML tags.

*/

var rootNode = "RequestResponse";

var fNode = "Field";

var docListNode = "DocList";

var docNode = "Doc";

var valueNode = "Value";

/*

* CSS variables

*/

var dataCSSClass = "row";

var headerCSSClass = "hdr";

/*

* Draw table header from xml document

*/

function drawHeader(tbody) {

var tr, td, i, j, oneRecord, flds;

tbody = document.getElementById(tbody);

// node tree

var data = xDoc.getElementsByTagName(rootNode)[0];

//alert(data);

for (i = 0; i < data.childNodes.length; i++) {

// use only 1st level element nodes

if (data.childNodes[i].nodeType == 1) {

// one bowl record

oneRecord = data.childNodes[i];

tr = tbody.insertRow(tbody.rows.length);

flds = oneRecord.getElementsByTagName(fNode);

for (j = 0; j < flds.length; j++) {

td = tr.insertCell(tr.cells.length);

td.setAttribute("id",headerCSSClass);

td.innerHTML = flds[j].firstChild.nodeValue;

}

}

}

}

/*

* Draw table data from XML data

*/

function drawData(tbody) {

var tr, td, i, j, k, oneRecord, flds, docs;

tbody = document.getElementById(tbody);

var data = xDoc.getElementsByTagName(rootNode)[0];

//alert(data.childNodes.length);

for (i = 0; i < data.childNodes.length; i++) {

if (data.childNodes[i].nodeName == docListNode) {

oneRecord = data.childNodes[i];

docs = oneRecord.getElementsByTagName(docNode);

for (k = 0; k < docs.length; k++) {

flds = docs[k].getElementsByTagName(valueNode);

tr = tbody.insertRow(tbody.rows.length);

for (j = 0; j < flds.length; j++) {

td = tr.insertCell(tr.cells.length);

td.setAttribute("id",dataCSSClass);

td.innerHTML = flds[j].firstChild.nodeValue;

}

}

}

}

}

/*

* Check that client browser supports XML

* and load external .xml file if its supported.

*/

function checkBrowserSupport(xFile) {

if (document.implementation && document.implementation.createDocument) {

xDoc = document.implementation.createDocument("", "theXdoc", null);

//xDoc = new XMLHttpRequest();

} else if (typeof ActiveXObject != "undefined") {

if (document.getElementById("msxml").async) {

xDoc = new ActiveXObject("Msxml.DOMDocument");

}

}

var ty = typeof xDoc.load;

if (xDoc && typeof xDoc.load != "undefined") {

// load external filefrom same domain)

xDoc.load(xFile);

return true;

} else {

var reply = confirm("This example requires a browser with XML support, such as IE5+/Windows or Netscape 6+.\n \nGo back to previous page?");

if (reply) {

history.back();

}

}

return false;

}

/* initialize first time - This method to be called when

* you want to start rendering XML

*/

function loadXML(xFile) {

// confirm browser supports needed features and load .xml file

if (checkBrowserSupport(xFile)) {

// Draw the header

setTimeout("drawHeader('header')", 1000);

//draw the data in table

setTimeout("drawData('data')", 1000);

}

}



This is how you can use it. The CSS file (XMLGenericDisplay.css) for the table is like this, you can change it as per your requirement.





#row {text-align:center;

color: #000000;

text-align:center;

background: url(http://4.bp.blogspot.com/_hpoWLUUuwbc/SHrhRrGZFwI/AAAAAAAAAK8/vfehDBVMWP8/S166/goldMo.JPG) repeat;

}

#hdr {

color: #ffff66;

text-align:center;

background: url(http://4.bp.blogspot.com/_hpoWLUUuwbc/SHvj5jkU4sI/AAAAAAAAALU/jQIGR6H0hqw/S163/bgMo.JPG) repeat;

}

table {

border: solid;

border-color:orange;

}



The XMLGenericDisplay.html file below calls the Javascript and displays the data in Response.xml file.



<html>

<head>

<title>Response</title>

<link rel="stylesheet" type="text/css" href="XMLGenericDisplay.css" />

<script type="text/javascript" src="XMLGenericDisplay.js"></script>

</head>

<body onload="loadXML('Response.xml');">

<h1>Response Data</h1>

<hr>

<table id="Response">

<thead id="header">

</thead>

<tbody id="data"></tbody>

</table>

<!-- Try to load Msxml.DOMDocument ActiveX to assist support verification -->

<object id="msxml" WIDTH="1" HEIGHT="1" classid="CLSID:2933BF90-7B36-11d2-B20E-00C04F983E60" ></object>

</body>

</html>



The Reponse.xml file looks like this.



<?xml version="1.0" encoding="UTF-8"?>

<RequestResponse>

<DocStructure>

<Field>FirstName</Field>

<Field>LastName</Field>

<Field>Address1</Field>

<Field>Address2</Field>

<Field>City</Field>

<Field>State</Field>

<Field>Country</Field>

</DocStructure>

<DocList>

<Doc>

<Value>Priya</Value>

<Value>D</Value>

<Value>add1</Value>

<Value>add2</Value>

<Value>Sunnyvale</Value>

<Value>CA</Value>

<Value>USA</Value>

</Doc>

<Doc>

<Value>Jini</Value>

<Value>Kolher</Value>

<Value>add1</Value>

<Value>add2</Value>

<Value>Chicago</Value>

<Value>IL</Value>

<Value>USA</Value>

</Doc>

<Doc>

<Value>Piyu</Value>

<Value>D</Value>

<Value>add1</Value>

<Value>add2</Value>

<Value>Windsor</Value>

<Value>CT</Value>

<Value>USA</Value>

</Doc>

</DocList>

</RequestResponse>





If your response XML file is different then you may edit the script and change the global variables for your specific node names in the XMLGenericDisplay.js file.



/*

Change below global variable values to match your XML tags.

*/

var rootNode = "RequestResponse";

var fNode = "Field";

var docListNode = "DocList";

var docNode = "Doc";

var valueNode = "Value";



Here is the snapshot of output



Embedding comments below your blogger post

I had been using the "Post Comment" link for quite sometime. Blogger (www.blogger.com) has also got the comment box feature added, which I didn't notice lately. Earlier only a link to "Post Comment" was displayed for commenting to a post. I am not sure if its really known to everyone so thought of posting it.

Here are the steps to create Embedded comment box below your post.

1. Login to http://draft.blogger.com , Yes this is the URL if you want embedded comments. You will not see the option for Embedded comment in www.blogger.com link.

2. Now go to your blog Settings tab.

3. Go to Comments
tab - here
select the option "Comment Form Placement" to be "Embedded below post" (It should look like the image displayed below)






4. Then go to the bottom of page and click "Save Settings" button.

5. Now view your blog, You will not see the embedded comment box here as its displayed with individual post so click on the Post Header Link and see if comment box is displayed.


Follow the below steps if you still don't see it.


5. Go to "Layout"

6. Go to "Edit HTML"


7. Check the "Expand widget templates" box.




8. Now find below code in the html template:

<p class='comment-footer'>
<a expr:href='data:post.addCommentUrl' expr:onclick='data:post.addCommentOnclick'>
<data:postCommentMsg/></a>
</p>



9. Then replace it with this code :

<p class='comment-footer'>
<b:if cond='data:post.embedCommentForm'>
<b:include data='post' name='comment-form'/>
<b:else/>
<b:if cond='data:post.allowComments'>
<a expr:href='data:post.addCommentUrl' expr:onclick='data:post.addCommentOnclick'>
<data:postCommentMsg/></a>
</b:if>
</b:if>
</p>

8. Click on "Save Template" button.

9. View the blog, now your comments should be embedded at the bottom of your posts.

Software Industry is not matured yet - SOA Hype is an example

I have been part of software industry for a decade now. I had always heard few statements by other engineering practice expert, specially Mechanical and Civil Engineering experts saying that

 

"Software industry is not as matured as Mechanical or Civil Engineering is."

 

Being part of this community, I was never able to accept this fact. In fact, I remember college days when I had read about reusability and other great practices adopted by our software industry. We are part of an industry which has grown extra ordinary in last century (rather few decades).  Great thing about our industry is the open source communities which are doing selfless work for whole world.

 

Right now I am working for a big networking company's IT department, and observed a heavy inclination of management towards Service Oriented Architecture (SOA).  I have read a whole bunch of articles on SOA on http://www.theserverside.com and many people have written about this as a hype of SOA architecture. I just wanted to write this post as it has not changed much despite lots of people trying to communicate the right way of utilizing such a beautiful architecture. My management seems so inclined to SOA that even our architects want to create services of almost everything. Sometimes we don't even understand why?

 

I am sure architects of our industry are not so immature that they would suggest it that way, but I can observe the pressure and expectation from top management, which forces good architects to do so. In my opinion top management thinks that you are a great architect if you create lots of services out of anything or everything.

 

            I have been to many interviews where job posting said they are looking for a "Software Architect", and as soon as they start interview they ask about SOA. I always wanted to say to them that only SOA is not Software architecture there are many other architectural solutions available and one solution doesn't fit all. What should be the first question (or important question) for such an interview, I assume "How would you chose a architectural solution?. Instead of asking that people start asking about WSDL tags and serviceregistry.xml, which annoys me even more.  I know there are many fake profiles floating around and its difficult to chose the right guy for your job, which forces interviewers to ask so specific questions about a technology, but what about asking fundamentals. I understood that they are not looking for a Software Architect but a SOA Architect.

 

I appeared for a niche segment consulting company, and they took 7-8 rounds of interviews starting from my fundamental approach to design till very specific questions of a technology like JSF, Struts. I am convinced that they should be niche segment consulting as they are very focused on selecting fundamentally strong people which is going to be their strength.

 

            People in this industry don't take things as seriously as it is taken in Civil or Mechanical engineering. A wrong choice of architecture would be fatal, and could harm human lives. Just imagine the architecture of Golden Bridge in SFO or Brooklyn Bridge in NYC, even after so many years of being built they still stand strong with great use.  I have never seen any "software" standing for more then 5 years like that. In fact that is the whole point that people in our industry don't take fundamental design and architecture concepts seriously as they know that collapse of it will not to be as hazardous as a collapse of a Bridge.

 

Now I agree that we are not as matured as other engineering practices, SOA is just another example. In fact we would never be so due to nature of our work.

 

What do you think? Let me know your thoughts.





Post Comment

Hiding Navigation bar in your Blog (Blogger)

The top navigation bar in blogger has some links like Flag blog, Search Blog and next blog for blogger.com. You may want to hide the navigation bar as you get more space to play with and also it will make your blog layout look better without a not matching dull blogger navigation barI am not sure if this breach of  the Term Of Service of blogger.com? But If you want to do this here's the trick to hide blogger navigation bar:

1. Login to Blogger then click "Layout --> Edit HTML".
2. Copy the code below and insert into your <head> tag.

#navbar-iframe {
display: none !important;
}

4. Now "Save" your settings, and view your blog, You will have no navbar in your blog page.
 
One important thing, if you do this trick then you will not see the Dashboard and login links, so you may need to to www.blogger.com for doing login and accessing dashboard.

Always switch job for only more money

I have always listened to many management guru's and my own bosses preaching me not switching job for just getting a salary hike.

Throughout my career in this software industry I have found bosses who talked like this. They always stressed focusing on skill set and type of work rather than money. I was impressed with those heavy words and never thought of switching job for a salary hike.

Now after few years of experience when I look around, it seems I am not getting paid anywhere close to people who had similar experience as me. When I think of them and compare myself, it seems only the paycheck is the biggest(rather huge) difference between us.

I had never thought that concentrating on skills and type of work would pay me so less. Why should I worry about skill set now, and anyways when I know that richest people all around the world are not great skilled people. Extra ordinary intelligent people also don't earn great money despite mastering many great skill sets. I am an average guy why would someone pay me more just for a skill which they can buy at anytime?

I guess I am slowly understanding the reason why people always focused towards money and moved on. Sometimes, very simple to understand things are also made complex by ourselves.
I have to ask just few fundamental questions to myself,

* What am I working for? Obviously money, then later comes the job satisfaction and blah blah blah...

* Am I being paid well? Off Course Not, because if I get paid well then how do the business makes money from me working for them?

I am definitely going to switch job for money now, and I think, all other things can be considered important after money is significant. Someday the company is going to fire employees like us and then I would repent that why didn't I switch my job! They are definitely going to hire or fire people just for money nothing else then why should I not do it as well.

I know this is not the right time for many of us to switch jobs, but if you are thinking of switching job I would suggest you to not think too much and should always do job switching for only money, rest all follows. What do you think?



job satisfaction statistics, switch job reason, why should I change my job

I want to become an author?

I always wanted to write something great whenever it was invented by me in day to day work. They call if best practices, gotchas, quick tricks. There are so many things I can suggest or share with people that can make their life easier. If this is what you also think. You have come to the right place.

We dont expect you to be great authors, you just need to be good at bringing articles which are values add for the software community.

Who are we?
We are a group of software professionals who are part of industry. Vision of this web site is to bring lots of good quality information free for the software community. Write an email to us if you want to be part of this group and become a author or contributor at Software Wikipedia site.

How do we publish?
Its very simple it doesn't take much to become a author here. If you can bring a article which is value add for the community then it will be published. If you continuously write good quality articles for us then we can give direct publishing rights to you as well. Write to us (software.wikipedia@gmail.com ) if you are interested to be an author at our site.

Rules and Guidelines for this forum.
We are interested in publishing articles which are a clear value add to the community. Any thing which is not a value add to community will not be published.

Interested for link exchange?
We can exchange links if our interests match, and our review team finds it a value add for the forum. If you are interested to exchange links of your sites/blogs with us then write to us at (software.wikipedia@gmail.com ).

Have suggestions/ideas?
We are open to ideas and suggestions and any idea for improvement is a great idea for us, feel free to write us at (software.wikipedia@gmail.com )


Contact us
We can be contacted by emails. You can email to us at (software.wikipedia@gmail.com ).

Heap Overflow Error in a Economic slowdown: Java Programmers Mind

Reading a lot about economic slowdown, it seems there are few companies who already started laying off people. Java programmers are also not going to be spared by brutal economic slowdown. I was thinking what would a Java programmer do if economy slows down more and there is not a software job to do. Then I thought I am good for nothing these days, I remember the days in college when I started coding in Pascal and was really content while wrote a small game of Tetris with my friend. That game always used to crash in between with an error called Heap Overflow Error, I never understood why that error was coming. I hated that error but had little time in the semester and had to move on the other languages for their share. I never saw that error in my programming life again. After so many great improvements in new application programming languages it was less headache for the programmers. Well, yes less headache means less efforts but now it seems I will never be comfortable writing a code on languages like Pascal. Who cares? and Why should I? 
 
I get a feeling, now I am programming in a air conditioned room for last 7 years and I have become good for nothing. What if I need to go back and start working under the sun for 2 hours, huh...it gets impossible, I cant even stand the heat. What if I need to walk for 2 miles and there is not Google map for directions? I have become to technology dependant that now I will not be able to do anything other than coding in Java.
 
What should be the alternate career option for a Java programmer?
 
Am I the only one in Java world who has got this feeling? or there are more geeks feeling good for nothing like me?
 
Now I realize why our Tetris program was giving Heap Overflow Error, I guess RAM was was trying to lay off a few useless guys from it but they were good for nothing like me and everything was overflowing, program crashed just like a bankruptcy.  (I know it doesn't make any sense....)
At this point in life again I am getting the same Heap Overflow Error in my brain, where my thoughts are overflowing with all negative vibes about bad economic conditions. Why cant I overcome these  heap overflow errors?
 
 
 
 


Have something to say? Post it here