You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Jim Clune <ji...@parasoft.com> on 2000/08/23 20:07:00 UTC

DocumentBuilder.parse(File) does not work correctly.

Summary:  DocumentBuilder.parse(File) does not work correctly.

I think this is a bug in the JAXP that is included with Xerces.

To reproduce:
Xerces-J:  xml-xerces_20000823101606.tar.gz
JDK: 1.2

Test case files:
foo.xml:
<?xml version="1.0"?>
<foo/>

ParseFileBug.java:
// Start ParseFileBug.java
import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;

public class ParseFileBug {
    public static void main(String args[]) {
        try {
            DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
         DocumentBuilder builder = factory.newDocumentBuilder();
      File file = new File("foobar/foo.xml");
         if (args.length == 0) {
             System.out.println("Parsing from File " + file);
             builder.parse(file);
         } else {
          InputSource inputSource = new InputSource(new
FileInputStream(file));
          inputSource.setSystemId("file:" + file.getAbsolutePath());
             System.out.println("Parsing from InputSource " + inputSource);
             builder.parse(inputSource);
         }
        } catch (Exception e) {
         e.printStackTrace();
         System.exit(1);
     }
     System.out.println("Success!");
    }
}
// End ParseFileBug.java

Test case:
Put ParseFileBug.java in a directory and compile it.
Make a sub-directory called "foobar" and put foo.xml in that
sub-directory.  From the original directory, run "java ParseFileBug".
(Make sure there is NOT a file called "foo.xml" in your current directory.)
You should get something like this:

Parsing from File foobar\foo.xml
org.xml.sax.SAXParseException: File
"file:///D:/home/holstein/jim/dv/test/foo.xml" not found.
    at
org.apache.xerces.framework.XMLParser.reportError(XMLParser.java:1014)
    at
org.apache.xerces.readers.DefaultEntityHandler.startReadingFromDocument(Defa
ultEntityHandler.java:512)
    at
org.apache.xerces.framework.XMLParser.parseSomeSetup(XMLParser.java:304)
    at org.apache.xerces.framework.XMLParser.parse(XMLParser.java:899)
    at
org.apache.xerces.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:12
3)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:171)
    at ParseFileBug.main(ParseFileBug.java:13)

Note:  This appears to be a problem only with the version of parse() which
takes
a file.  If I convert the file to an InputSource, then parse that, it works
fine.
To see this, run the test program with an argument:
java ParseFileBug InputSource
Parsing from InputSource org.xml.sax.InputSource@3fce090b
Success!

Regards,

Jim Clune
jim@parasoft.com



Re: DocumentBuilder.parse(File) does not work correctly.

Posted by Mikael St�ldal <d9...@d.kth.se>.
In article <01...@holstein>,
jim@parasoft.com (Jim Clune) wrote:

>      File file = new File("foobar/foo.xml");

This might not be your problem, but:

Do you have an operating system which uses / as directory separator
(e.g. UNIX)? If you use e.g. Windows, you should specify the path as
"foobar\\foo.xml".

-- 
/****************************************************************\
* You have just read a message from Mikael Ståldal.              *
*                                                                *
* Remove "-ingen-reklam" from the address before mail replying.  *
\****************************************************************/