标题:求高手帮忙,关于servlet中用dom4j解析xml后向浏览器输出数据
只看楼主
吴辉
Rank: 3Rank: 3
来 自:湖南
等 级:论坛游侠
帖 子:52
专家分:199
注 册:2011-3-27
结帖率:75%
已结贴  问题点数:20 回复次数:5 
求高手帮忙,关于servlet中用dom4j解析xml后向浏览器输出数据
package com.hnuc.wuhui.controller;
import
import
import
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.

public class CookieDemo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.write("------还没好---"); //这行这浏览器能输出
        SAXReader reader = new SAXReader();
        try {
            Document document = reader.read(new File("/WEB-INF/Book.xml"));   //这里路径该怎么写?
            Element root = document.getRootElement();
            for (Iterator i = root.elementIterator("book"); i.hasNext();) {
                Element abook = (Element) i.next();

                out.write(abook.attributeValue("bookid") + "<br/>");
                out.write(abook.elementText("name") + "<br/>");
                out.write(abook.elementText("price") + "<br/>");
                out.write(abook.elementText("description") + "<br/>");

            }

        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

dom4j的jar也导入了工程,添加了编译路径。
发布工程,启动tomcat后用浏览器访问该servlet,报错了...
org.dom4j.DocumentException: \WEB-INF\Book.xml (系统找不到指定的路径。) Nested exception: \WEB-INF\Book.xml (系统找不到指定的路径。)
    at org.(SAXReader.java:266)
    at com.hnuc.wuhui.controller.CookieDemo1.doGet(CookieDemo1.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.(AprEndpoint.java:1815)
    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:619)
Nested exception:
\WEB-INF\Book.xml (系统找不到指定的路径。)
    at (Native Method)
    at (FileInputStream.java:106)
    at org.(SAXReader.java:243)
    at com.hnuc.wuhui.controller.CookieDemo1.doGet(CookieDemo1.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.(AprEndpoint.java:1815)
    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:619)
Nested exception: \WEB-INF\Book.xml (系统找不到指定的路径。)
    at (Native Method)
    at (FileInputStream.java:106)
    at org.(SAXReader.java:243)
    at com.hnuc.wuhui.controller.CookieDemo1.doGet(CookieDemo1.java:32)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
    at org.apache.tomcat.(AprEndpoint.java:1815)
    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:619)


求高手指点。。。
搜索更多相关主题的帖子: servlet 浏览器 package public import 
2012-05-02 15:40
吴辉
Rank: 3Rank: 3
来 自:湖南
等 级:论坛游侠
帖 子:52
专家分:199
注 册:2011-3-27
得分:0 
Document document = reader.read(new File("/WEB-INF/classes/Book.xml"));   //这样也不行?
  是不是servlet里面不能这样解析呀?
  求高手帮忙。。。
2012-05-02 15:54
lucky563591
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:765
专家分:2103
注 册:2009-11-18
得分:14 
没有找到xml文件。
2012-05-03 11:01
吴辉
Rank: 3Rank: 3
来 自:湖南
等 级:论坛游侠
帖 子:52
专家分:199
注 册:2011-3-27
得分:0 
回复 3楼 lucky563591
可一说明白点吗?
2012-05-04 15:40
吴辉
Rank: 3Rank: 3
来 自:湖南
等 级:论坛游侠
帖 子:52
专家分:199
注 册:2011-3-27
得分:0 
我把路径改成myeclipse工作间工程目录下的xml文件,能行。
Document document = reader.read(new File("F:\\MyEclipse 8.6\\day07\\src\\Books.xml"));

问题是,假如我发布到远程服务器呢?怎么办?
2012-05-04 16:29
高寒
Rank: 2
等 级:论坛游民
帖 子:134
专家分:20
注 册:2005-10-25
得分:0 
首先,要用response.reset();
其次,加载xml文件可以用Thread.currentThread().getContextClassLoader().getResourceAsStream(你的xml文件路径)
最后,用response写到客户端

2012-05-11 15:11



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-366831-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.354942 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved