2012-08-02 95 views
0

我陷入了servlet中的DOM解析器。這裏是代碼:DOM解析器在servlet中失敗

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 

有人可以幫pleae我在這。我堅持這一點,不能繼續。

在此先感謝。

回答

0

演員,因爲你正在使用

import javax.lang.model.element.Element; 

代替

import org.w3c.dom.Element; 

正確(乾淨)import語句失敗。 javax.lang.model.element.Element與DOM元素無關,將其刪除。

+0

非常感謝...它的工作.. :) – user443208 2012-08-02 09:54:35

+0

接受..再次感謝對我的問題的快速反應。 – user443208 2012-08-02 10:34:56