I have a servlet that reads data out of a text file and sends that data as an xml page. The servlet is hosted on http://localhost:8080/Checkers/CheckersServlet. I made it in Eclipse IDE for Java EE and host it on a Tomcat server. If I open that page in my browser it displays the appropriate xml document. I have some ajax code whose goal is to query the server, get the xml file, and read it into a table. However all of the GetElementsByTagName() methods return empty lists, specifically users, wins, and losses in the html code. I have tried the following things which might be suggested:

  • Using var doc = req.responseXML instead of req.responseText and parsing it
  • Replacing the code for reading out the text file with hard coded data



I have no idea why this code isn't working. Thanks for any insights you may give.

By replacing xmlDoc.getElementsByTagName('Username') with xmlDoc.getElementsByTagName('*') I have found out that there are only 2 tags in the xmlDoc file, which should not be the case. When I try to display the contents of this first item I get the following error: XML Parsing Error: no element found Location: file:///Users/adamsturge991/Desktop/CheckersScoreboard.html
Line Number 1, Column 1

Servlet code

import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
/**
 * Servlet implementation class CheckersServlet
 */
@WebServlet("/CheckersServlet")
public class CheckersServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
 
/**
 * @see HttpServlet#HttpServlet()
 */
public CheckersServlet() {
    super();
    // TODO Auto-generated constructor stub
}
 
/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    // TODO Auto-generated method stub
    response.setContentType("text/xml");
    PrintWriter out = response.getWriter();
    out.println("<?xml version='1.0'?>" );
 
    BufferedReader fReader = new BufferedReader(new FileReader("/Users/adamsturge991/desktop/checkersdatabase.txt"));
    String data = fReader.readLine();
    out.println("<CheckersData>");
    while(data != null) //read a user name
    {
        out.println("<User>");
        out.println("<Username>");
        out.println(data); 
        out.println("</Username>");
        data = fReader.readLine();
        out.println("<Password>");
        out.println(data);
        out.println("</Password>");
        data = fReader.readLine();
        out.println("<Wins>");
        out.println(data);
        out.println("</Wins>");
        data = fReader.readLine();
        out.println("<Losses>");
        out.println(data);
        out.println("</Losses>");
        out.println("</User>");
        data = fReader.readLine(); //get next username or null if end of database
    }
    out.println("</CheckersData>");
}
 
/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    }
 
}

HTML code

HTML Code:
<html>
    <head>
    <script type="text/javascript">
    function requestUpdate()
    {
        var req = new XMLHttpRequest();
        req.onreadystatechange = function()
        {   
            if(req.readyState == 4)
            {
                displayResult(req);
            }
        }
        var url = "http://localhost:8080/Checkers/CheckersServlet"; 
        req.open("GET",url,true);
        req.send(null);
    }

    function displayResult(req)
    {
        var div = document.getElementById("Scoreboard");
        div.innerHTML = "<table border> <tr> <th> Username </th> <th> Wins </th> <th> Losses </th> </tr>" ;

        var doc = req.responseText;
        var parser= new DOMParser();
        var xmlDoc = parser.parseFromString(doc,"text/xml");
        var Users = xmlDoc.getElementsByTagName('Username');
        var Wins = xmlDoc.getElementsByTagName('Wins');
        var Losses = xmlDoc.getElementsByTagName('Losses');
        var U;
        var W;
        var L;
        alert(Users.length);
        for(var i = 0; i < Users.length; i++)
        {
            U = Users.item(i).textContent;
            W = Wins.item(i).textContent;
            L = Losses.item(i).textContent;

            div.innerHTML = div.innerHTML + "<tr> <td>" + U + "</td> <td>" + W + "</td> <td>" + L + "</td> </tr>";
        }

    div.innerHTML = div.innerHTML + "</table>";
    }
    </script>
    </head>
    <body onload = "requestUpdate()">
        <button onclick = "requestUpdate()">Update</button>
        <div id = "Scoreboard"></div>
    </body>
</html>