org.xml.sax.SAXParseException: Relative URI "student.dtd"; can not be resolved without a base URI.这个应该是没有把DTD和XML文件放在正确的目录下面造成的,在你的XML文件中,类型声明是这样的:<!DOCTYPE students SYSTEM "student.dtd">
所以呢,DTD和XML应该放在同一目录下面。

解决方案 »

  1.   

    谢谢~~
    但是我的xml和dtd文件是在同一层目录里.所以应该不是这个原因~~
    说明:我把所有原文件复制出来,在命令行里可以编译执行,同时也解析出了xml文件里的内容.
    但同样的原文件在eclipse里就会报这个错!!!!!
    请问有什么解决办法??????
      

  2.   

    说明:我把所有原文件复制出来,在命令行里可以编译执行,同时也解析出了xml文件里的内容.
    =============================
    这就没问题了,有Eclipse里会出错,因为Eclipse运行时的起始目录和命令行执行不同!
      

  3.   

    或者,能不能说一下,我该怎么做才不会在eclipse时报错.
      

  4.   

    大致的意思是这样的:
    Eclipse项目的目录可以设置成源文件和Class文件单独存放(src目录和bin目录),也可以设置成都放在同一目录中。
    假如说是前者,源文件放在src目录,Class文件放在bin目录,那么我们命令行执行的时候就要进入到bin目录执行,这个时候所有的其他相关文件都是相对于bin目录来说的,如果你放在项目的根目录里面(即与src和bin目录在同一目录下),命令行就会出错,但是eclipse可能不会出错,因为他的起始目录就是项目的根目录,所以能够保证找到相关的文件。
    可以做个简单的测试:
    新建一个Java项目,把源文件和Class文件设置成放置在单独的目录里,然后在项目根目录下随便放个文本文件(test.txt),然后写个主类来读这个文件。
    // Test.java -- 
    // 2007-01-02 12:18import java.io.*;public class Test { public static void main(String[] args) {
    try {
    BufferedReader in = new BufferedReader(new FileReader("test.txt"));
    String buf;

    while((buf = in.readLine()) != null) {
    System.out.println(buf);
    }
    }
    catch(FileNotFoundException e) {
    e.printStackTrace();
    }
    catch(IOException e) {
    e.printStackTrace();
    }
    }
    }1. 使用Eclipse运行,可以正确运行,输出文件内容。
    2. 命令行运行:抛出FileNotFoundException。
    由此可见,两者的运行时上下文是不同的,所以LZ的问题应该也是这一类问题,可能具体不同,但都是由于运行时上下文不同造成的。反过来,你把test.txt文件放在src目录里面(编译后会在bin目录里),这时来运行,命令行可以正确运行,Eclipse又抛出FileNotFoundException了。================================
    总结:Eclipse的运行时起始目录是Eclipse项目的根目录;命令行的话,当然你在哪个目录下执行命令,哪个目录就是起始目录了。有问题的话不好意思哈!!!^_^
      

  5.   

    感谢你的解答,不过,错误好像不是因为这个:
    我的eclipse设置的是源代码放在src目录和class文件放在bin目录xml和dtd文件都放在了项目的根目录.但在eclipse是在这行代码的异常导致的:
        in = new FileInputStream("./student.xml");
        doc = docBuilder.parse(in);
    org.xml.sax.SAXParseException: Relative URI "student.dtd"; can not be resolved without a base URI.
    我在控制台测试是把所有源文件和xml、dtd文件都放在了同一个目录下,测试通过.
      

  6.   

    谢谢e_ville,现在问题已经解决:
    try{
        docBuilder = domfac.newDocumentBuilder();
        in = new FileInputStream("student.xml");
        doc = docBuilder.parse(in);
        root = doc.getDocumentElement();
    .......修改为:
    try{
         docBuilder = domfac.newDocumentBuilder();
         url = this.getClass().getResource("student.xml");
         doc = docBuilder.parse(new File(url.getFile()));这样就行了.
    现在散分喽!!
      

  7.   

    Your XML document makes reference to a DTD through a relative URI or path and 
    the XML parser is unable to resolve this relative URI from the XML document 
    path, i.e. relatively to "d:/XMLTest/xml".There are several solutions to your problem :1. Simple: Make sure the DTD file can be found locally, relatively to the XML 
    file path. If necessary, if you are using a stream/reader as input, you can 
    use SAXBuilder.build(InputStream/Reader, String) or set the System ID in the 
    SAX InputSource to let the parser know where the XML document actually lies 
    (this corresponds to setting the "base URI" the parser is asking for).2. More generic: Implement an org.xml.sax.EntityResolver and register it onto 
    SAXBuilder to request the parser to delegate to your application the 
    resolution of enternal entities. That way, you are free to store the DTD files 
    whereever you wish (e.g. in the application JAR) or manage a cache of 
    frequently accessed DTDs.