关于dom4j怎么读取xml文件。。在jsp页面!急
首先我写了一个方法,用dom4j拿到mbs.xml文件,又写了一个测试文件(test.java),看这个方法能不能用,这个test.java。里面用的是main方法输出的。没有问题,控制台能输入xml里的值。,后来,我又写了几个jsp和servlet,想把这个xml的值,在页面上输入出来,报错,说找不到xml文件了,这个用jsp怎么写。
先贴几个代码,谢谢了
这是显示的那个servlet代码,用来在页面上输入的。
Message m = new Message(); LookMessage lm = new LookMessage(); List list = new ArrayList(); try { list = lm.getMessage(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } out.println("<title>查看留言</title>"); out.println("<table align=center border=1"); out.println("<tr><td colspan=3><font color=red>已发表留言有</font></td></tr>"); out.println("<tr><td><font color=red>姓名</font></td></tr><td><font color=red>内容</font></td><td><font color=red>时间</font></td>"); for(int i=0;i<list.size();i++){ m = (Message) list.get(i); out.println("<tr><td>"+m.getName()+"</td></tr>"); out.println("<tr><td>"+m.getContents()+"</td></tr>"); out.println("<tr><td>"+m.getDate()+"</td></tr>"); } out.println("</table>");
public List getMessage() { // TODO Auto-generated method stub List list = new ArrayList(); StringBuffer sb = new StringBuffer(); SAXReader reader = new SAXReader(); Document doc = null; try { doc = reader.read(new File("mbs.xml")); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } Element root = doc.getRootElement(); List nodes = root.elements("value"); for(Iterator it = nodes.iterator();it.hasNext();){//循环有几个"value"节点 Element elm = (Element) it.next(); Message m = new Message(); for(Iterator itr = elm.elementIterator();itr.hasNext();){//循环“value”下面的节点 Element e1 = (Element) itr.next(); m.setName(e1.getText()); Element e2 = (Element) itr.next(); m.setContents(e2.getText()); Element e3 = (Element) itr.next(); m.setDate(e3.getText()); list.add(m); // System.out.println("根节点"+e.getText()); } } return list; }
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
Nested exception:
java.io.FileNotFoundException: mbs.xml (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:243)
at com.java.mge.LookMessage.getMessage(LookMessage.java:30)
at com.java.mge.LookM.doGet(LookM.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
Nested exception: java.io.FileNotFoundException: mbs.xml (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at org.dom4j.io.SAXReader.read(SAXReader.java:243)
at com.java.mge.LookMessage.getMessage(LookMessage.java:30)
at com.java.mge.LookM.doGet(LookM.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
java.lang.NullPointerException
at com.java.mge.LookMessage.getMessage(LookMessage.java:36)
at com.java.mge.LookM.doGet(LookM.java:53)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
[解决办法]
new File("mbs.xml")中文件地址不对。。。获取xml文件正确的存放路径。。。