Monday, May 18, 2009

Parse XML file in JavaScript part 2

As one of reader asked about how to handle XML respone if we get more than records or if your XML file have many child nodes.
I gave here one complete example with XML file and JavaScript in JSP.

1. abc.xml

<Name>Binod Kumar Suman</Name>

<Student> <Name>Pramod Kumar Modi</Name>

<Name>Sanjay Kumar</Name>

<Name>Mukesh Ambani</Name>


2. StudentInfo.jsp

<html><head><title>Binod Java Solution AJAX </title>
<script type="text/javascript">

function showResult(){

var xmlDoc=new ActiveXObject("MSXML.DOMDocument");
xmlDoc.async="false"; xmlDoc.load("abc.xml");
var students=xmlDoc.documentElement;
var student = students.childNodes(0);
var rows = students.childNodes.length; v
ar cols = student.childNodes.length;

var body = document.getElementsByTagName("body")[0];
//var tabl = document.createElement("table");
var tabl = document.getElementById("studentinfo");
var tblBody = document.createElement("tbody");

for (var i = 0; i < rows; i++) {
var row = document.createElement("tr");
student = students.childNodes(i);

for (var j = 0; j < cols; j++) {
var cell = document.createElement("td");
var cellText = document.createTextNode(student.childNodes(j).firstChild.text); cell.appendChild(cellText);

tabl.setAttribute("border", "2");


</head><body onload="showResult()">
<h2> GET STUDENT INFO </h2><br>

<table id="studentinfo"> <tr bgcolor='red'> <td >Name</td><td>Hostel</td><td>Contact</td> </tr> </table>

Now your html table will grow dynamically as per number of records in XML file.


  1. why its not working on mozilla?

  2. there is parse error in your file.
    Line 10 (there is a "v" at the end which is supposed to be "var" in the following line) and
    i had to put a "}" just before the "/script" tag to get the thing working. Thank you anyway.

  3. I fixed everything according to previuos post, but it still doesn't work in Firefox

  4. if (window.XMLHttpRequest) { // Mozilla, Safari, ...
    httpRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");

    probably because of this try it

  5. yes, it still doesnt work in firefox

  6. That's because this line:

    var xmlDoc=new ActiveXObject("MSXML.DOMDocument");

    is specific for IE. For non-IE browsers you probably want to use the DOM Parser:

  7. Thanks, your article helped me a lot - I now more or less understand how to carry on with XML and Javascript. Good job!

  8. code is not working on mozilla

  9. function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
    } catch (e) {
    // Internet Explorer
    try {
    xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

    return xmlHttp;

  10. its not working in neither of browsers....wat might be the problem??plzz reply me to my emailid

  11. this code isn't working in any of my browsers i donno wat is the problem ...please get me out of this problem ....plzz

  12. thanks you man .... you saved my life :) just the thing I was looking for ..

  13. Very nice.Please keep visit this blog for good tutorial on Java.


You can put your comments here (Either feedback or your Question related to blog)