Does a java SAX parser actually retrieve the DTD from the internet?
Asked Answered
T

3

6

With an XML file that declares a DOCTYPE, take the struts.xml file for instance:

Does the Java SAX processing actually go get the dtd from the provided URL?

Trevatrevah answered 30/11, 2012 at 22:4 Comment(0)
E
4

Many (most) implementations will, even though technically, if I remember correctly, the location is meant to be interpreted as a URI, not URL.

If you want to be sure to resolve it locally, take a look at the EntityResolver interface.

Extend answered 30/11, 2012 at 22:11 Comment(2)
So, where would it find the dtd prior to the internet? I mean, the doctype reference is an http url; is there a logic to convert this to a classpath lookup? I noticed the struts jars all have the dtd's inside of them.Trevatrevah
Well, DTDs didn't exist before the internet :-) Anyway, the doctype reference looks like an http URL, but that's just convention - it's really a URI (there is a difference), and does not necessarily map to a valid URL. I haven't used Struts in many years, but I'm guessing that it includes the DTDs and uses an EntityResolver to resolve them locally.Extend
S
3

Based on the Xerces documentation, the parser may try to download the file from the internet when validation is turned on. But I really think this can change from parser to parser, because the library may decide to use a local cache, a proxy server or whatever.

Selfsuggestion answered 30/11, 2012 at 22:14 Comment(2)
I saw that. I couldn't find any more specifics about where else, and in what order, it would attempt to find the dtd. Filesystem? Classpath?Trevatrevah
I think, since this is an implementation details, there's not better documentation that the source code and a debuggerSelfsuggestion
R
0

You can modify the struts.xml to load the DTD file from the struts2 core jar from the classpath rather than loading it from internet

FROM

<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts 
Configuration//EN" 
"http://struts.apache.org/dtds/struts-2.0.dtd"> 

CHANGE TO

<!DOCTYPE struts SYSTEM "struts-2.0.dtd">

now the sax parser loaded the struts-2.0.dtd file from the /src folder where i placed.

Also this is my modified hibernate configuration file which worked a little differently. It loaded the dtd file directly from the JAR, did not have to physically place it in the src folder

<!DOCTYPE hibernate-configuration SYSTEM "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
Rolanda answered 14/3, 2018 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.