web项目,流程是:
      页面(有参数)-----》servlet----调用-----》Manager类(操作数据)---首先要调用--》ParseXML类(读取xml信息) 
   
 此xml文件在src目录里。。解析xml文件用dom4j和xpath解析在parseXML类中我本来写的是
                                          File file = new File("src/xx.xml")
                                          Document doc = new SAXReader().read(file);然后我写了一个测试类读取ParseXML可以执行。。但我从页面调用servlet就是找不到这个文件。(解决方法是用类装载器)但想请高人教下从页面读取和自己写一个测试类读取有什么不一样。为什么一个能找到一个找不到。

解决方案 »

  1.   

    这个原因是因为你采用的相对路径的办法,new File("src/xx.xml")这个路径是以系统属性system.userdir为基路径,如果在测试类当中,基路径是工程的根目录,而在web当中调用servlet调用的时候基路径发生了改变,所以找不到xx.xml文件
    解决方案也不是一定要用什么类装载器,只是避开用这种相对路径的方法,Class.getResourceAsStream()方法也可以用来解决问题
      

  2.   

    从servlet访问最终不还是用ParseXML类来解析的么。基路径变成什么了。可以详细说说么。
    另外。。Class.getResourceAsStream()好像也是参照类装载器来实现的
      

  3.   

    单独测试能找到,因为你的项目/src下有这个xml文件
    servlet中为啥找不到,是因为你将项目部署到服务器之后还有src这个目录吗,src目录下的东西全到
    项目/WEB-INF/classes目录里面了
      

  4.   

    服务器是没有src的,src里的java文件都编译成class发布到 项目/WebRoot/WEB-INF/classes里的 你把xml放到项目/WebRoot/目录里面吧。
      

  5.   

    这就是答案·楼主没有去翻看tomcat下编译了的项目的部署情况过么?
      

  6.   

     改成:File file = new File("classpath:xx.xml")试试吧!
    你写测试类访问时src是物理存在的;
    但是部署时没有src路径,想想你在类中引入另一个类时写import com.test.UserBean,而不是写import src.com.test.UserBean
    (I think!)
      

  7.   

    解析的代码,如下:
    public class ParseConfigXml { /**
     * @param args
     */
    public static void main(String[] args) {
    String url = "/config.xml";
    ParseConfigXml.parseXmlData(url);
    } /**
     * @method   getParseXmlDocument
     * @param parseXmlPath 待解析的xml文件路径,该路径必须要在classpath中能找到
     * @throws DocumentException 
     * @方法描述:获取指定xml文档的对象document
     */
    public static Document getParseXmlDocument(String parseXmlPath) {
    if(parseXmlPath == null) {
    return null;
    }

    SAXReader reader = new SAXReader();
    Document document = null;
    try {
    InputStream inputStream = ParseConfigXml.class.getResourceAsStream(parseXmlPath);
    document = reader.read(inputStream);
    return document;
    } catch(DocumentException de) {
    System.out.println("读取classpath下的xml文档路径发生异常");
    return null;
    }
    }

    public static List<String> parseXmlData(String parseXmlFile) {

    // 将xml文档转换为Document对象
    Document document = getParseXmlDocument(parseXmlFile);
    Element root = document.getRootElement();
    // System.out.println(root.getName());
    StringBuffer sb = new StringBuffer();
    sb.append("通过Dom4j解析XML,并输出数据:\n");
    sb.append("----------------遍历start----------------\n");
    List<String> ids = new ArrayList<String>();
    for(Iterator<?> it = root.elementIterator(); it.hasNext(); ) {
    Element essp = (Element) it.next();
    String value = essp.getName();
    System.out.println(value);
    if("essp".equals(value)) {
    Element valueElement = root.element("essp");
    for(Iterator<?> valueIt = valueElement.elementIterator(); valueIt.hasNext(); ) {
    Element valueEle = (Element) valueIt.next();
    sb.append(valueEle.getName() + " = " + valueEle.getText() + "\n");
    ids.add(valueEle.getText());
    }
    }
    }
    sb.append("-----------------遍历end-----------------\n");
    // System.out.println(sb.toString());
    return ids;
    }
    }
    src目录下的xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <config>
    <essp id="esspID">
    <value name="id">WH1102015</value>
    <value name="id">WH0402001</value>
    <value name="id">WH0405002</value>
    <value name="id">WH0406003</value>
    <value name="id">WH0406008</value>
    <value name="id">WH0406011</value>
    <value name="id">WH0512002</value>
    <value name="id">WH0605005</value>
    <value name="id">WH0606003</value>
    <value name="id">WH0607009</value>
    <value name="id">WH0610010</value>
    </essp>
    </config>