I'm using Java's built in DocumentBuilder parser on an XHTML document but keep running into this message:
Could not read source file: Server returned HTTP response code: 503 for URL
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdThe document is actually stored in a locally stored file and I don't want to validate it with the online DTD so I'm hoping there is a way to supress the parser's attempts to get the DTD.
The following is the code I use to parse the document from the file. As you'd guess, I call this method giving it new FileInputStream(InputFile).
[code]
public static Document parse(InputStream in)
{
DocumentBuilder docBuilder;
Document doc = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
}
catch (ParserConfigurationException e) {
System.out.println("Wrong parser configuration: " + e.getMessage());
return null;
}
try {
doc = docBuilder.parse(in);
}
catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
}
catch (IOException e) {
System.out.println("Could not read source file: " + e.getMessage());
}
return doc;
}
[/code]
I don't want to validate the document to its DTD and given the overhead involved, I really wonder why someone picked this as default behaviour.
The short question is, can I prevent the parser from even trying to get the DTD?
Comments
: document but keep running into this message:
:
: Could not read source file: Server returned HTTP response code: 503
: for URL http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd
:
: The document is actually stored in a locally stored file and I don't
: want to validate it with the online DTD so I'm hoping there is a way
: to supress the parser's attempts to get the DTD.
:
: The following is the code I use to parse the document from the file.
: As you'd guess, I call this method giving it new
: FileInputStream(InputFile).
: [code]:
: public static Document parse(InputStream in)
: {
: DocumentBuilder docBuilder;
: Document doc = null;
: DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
: docBuilderFactory.setIgnoringElementContentWhitespace(true);
: try {
: docBuilder = docBuilderFactory.newDocumentBuilder();
: }
: catch (ParserConfigurationException e) {
: System.out.println("Wrong parser configuration: " + e.getMessage());
: return null;
: }
: try {
: doc = docBuilder.parse(in);
: }
: catch (SAXException e) {
: System.out.println("Wrong XML file structure: " + e.getMessage());
: return null;
: }
: catch (IOException e) {
: System.out.println("Could not read source file: " + e.getMessage());
: }
: return doc;
: }
: [/code]:
:
: I don't want to validate the document to its DTD and given the
: overhead involved, I really wonder why someone picked this as
: default behaviour.
:
: The short question is, can I prevent the parser from even trying to
: get the DTD?
Use a Schema to validate the document:
- Set validating to false
- Load a new schema into the DocumentBuilderFactory using SchemaFactory.newSchema(File)
This should stop the on-line request for a DTD and load a local file as the DTD.