0

我被 servlet 中的 DOM 解析器困住了。这是代码:

    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();
              }

}

当我将它作为普通的 java 代码运行时,此代码工作正常。但是,如果我将相同的 java 代码放在 servlet 中,则这行代码会出现以下错误: 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

有人可以帮我解决这个问题吗?我被困住了,无法继续。

提前致谢。

4

1 回答 1

0

演员失败,因为你正在使用

import javax.lang.model.element.Element;

代替

import org.w3c.dom.Element;

更正(并清理)您的导入语句。javax.lang.model.element.Element 与 DOM 元素无关,删除它。

于 2012-08-02T09:32:16.780 回答