Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8325603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:29:39+00:00 2026-06-09T00:29:39+00:00

I am stuck with the DOM parser in a servlet. Here is the code:

  • 0

I am stuck with the DOM parser in a servlet. Here is the 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 < 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< 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();
              }

}

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;

    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

Can somebody pleae help me out in this. I am stuck with this and not able to proceed.

thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:29:41+00:00Added an answer on June 9, 2026 at 12:29 am

    The cast fails because you are using

    import javax.lang.model.element.Element;
    

    instead of

    import org.w3c.dom.Element;
    

    Correct (and clean) your import statements. javax.lang.model.element.Element has nothing to do with DOM elements, remove it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am stuck while escaping < and > in the xml file using xml.dom.minidom.
I want to change an XML file. I'm using DOM Parser. My XML file
I am parsing a XML file in Java using the W3C DOM. I am
I am trying to parse through a document using a DOM Parser in java.
I'm trying to follow the tutorial here . I'm stuck on the section DOM
Kinda stuck here... I have an application with lets say 5000 rows of data
Got stuck here: http://jsfiddle.net/UFkg8/ Right now the animation is top-down. What do I need
I have a huge XML(>400MB) containing products. Using a DOM parser is therefore excluded,
I'm stuck using native DOM methods (I know, right?) and I have a structure
i'm stuck with the subj question. this is the sample code: include('./simple_html_dom.php'); $html =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.