title: DOM parser failed in a servlet
body: <p>I am stuck with the DOM parser in a servlet. Here is the code: </p>
  
  <pre><code>    import java.awt.List;
      import java.io.File;
      import java.io.IOException;
      import java.io.PrintWriter;
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.Enumeration;
      import java.util.HashSet;
  
      import javax.lang.model.element.Element;
      import javax.servlet.RequestDispatcher;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      import javax.swing.text.Document;
      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
  
      import org.w3c.dom.Node;
      import org.w3c.dom.NodeList;
  
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.DocumentBuilder;
  
      import org.w3c.dom.NodeList;
      import org.w3c.dom.Node;
  
      import java.io.File;
      import java.util.ArrayList;
  
  
      import java.io.IOException;
      import java.io.PrintWriter;
      import java.sql.Connection;
      import java.sql.PreparedStatement;
      import java.sql.ResultSet;
      import java.sql.SQLException;
      import java.util.ArrayList;
      import java.util.Collections;
      import java.util.Enumeration;
      import java.util.HashSet;
  
      //import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
  
      public class RoutingParser extends HttpServlet {
  
  
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
       response.setContentType("text/html");
       PrintWriter writer = response.getWriter();
  
       System.out.println("Routing Parser is accessible");
  
       System.out.println("i reached servlet");
            try {
  
                ArrayList inputlist1= new ArrayList();
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("D");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("N");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("US")
                  inputlist1.add("%");
                  inputlist1.add("%");
                  inputlist1.add("%");
  
  
                  ArrayList list1=new ArrayList();
  
                  System.out.println("i reached servlet");
                    try {
  
                      File fXmlFile = new File("c:\\eclipse\\RoutingTable.xml");
                      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                      org.w3c.dom.Document doc = dBuilder.parse(fXmlFile);
                      doc.getDocumentElement().normalize();
  
                      System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
                      NodeList nList = doc.getElementsByTagName("Entry");
                      System.out.println("-----------------------");
  
                      for (int temp = 0; temp &lt; nList.getLength(); temp++)
                      {
  
                         Node nNode = nList.item(temp);
                         System.out.println("temp value is: " + temp);
                         if (nNode.getNodeType() == Node.ELEMENT_NODE) 
                         {
  
                            Element eElement = (Element) nNode;
  
                            //some piece of code related to arraylist
  
  
                            System.out.println("checking the node number::::::::::: " + temp);
  
                            int match_count=0;
                            //loop around the internal elements and check for equality.
                            for (int temp1=0; temp1&lt; 25; temp1++)
                            {
                                //some piece of code to validate data
                            }
  
                            //if the count is 25, it means it has not broken due to inequality in between and thus return the authorizer in that particular node.
                            if (match_count == 25){
                               //some piece of code for more validation
                            }
                            else{
                                System.out.println("This combination is not supported");
                            }
  
                         }
  
  
                      }
                    } catch (Exception e) {
                      e.printStackTrace();
                    }
  
                    System.out.println("array list in end is: " + list1);
            } finally {
                System.out.println("in final block");
            }
                }
  
                private static String getTagValue(String sTag, Element eElement) {
                  NodeList nlList = ((org.w3c.dom.Document) eElement).getElementsByTagName(sTag).item(0).getChildNodes();
  
                      Node nValue = (Node) nlList.item(0);
  
                  return nValue.getNodeValue();
                }
  
  }
  </code></pre>
  
  <p>This code works fine when i run it as a normal java code.
  However if i put the same java code in a servlet i get following error for this line of code: Element eElement = (Element) nNode;</p>
  
  <pre><code>    INFO: Server startup in 744 ms
      Routing Parser is accessible
      i reached servlet
      i reached servlet
      Root element :Data
      -----------------------
      temp value is: 0
      java.lang.ClassCastException:         com.sun.org.apache.xerces.internal.dom.DeferredElementImpl cannot be cast to javax.lang.model.element.Element
  at RoutingParser.doGet(RoutingParser.java:116)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:403)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:301)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:162)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:140)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  at java.lang.Thread.run(Thread.java:662)
      array list in end is: []
      in final block
  </code></pre>
  
  <hr>
  
  <p>Can somebody pleae help me out in this. I am stuck with this and not able to proceed.</p>
  
  <p>thanks in advance.</p>