OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] How to open XML file that reference entity resolver (DTD)

Hi Liam,
I took up your 3rd suggestion by overriding the default entity resolver by creating LocalDTDResolver.java as follows:
    public class LocalDTDResolver implements EntityResolver {
        String mySystemIdToIntercept;
        File myLocalDtdPath;
        URL localDtdFileAsUrl;
        public LocalDTDResolver( String systemIdToIntercept, File localDtdPath ) throws MalformedURLException {
            mySystemIdToIntercept = systemIdToIntercept;
            myLocalDtdPath = localDtdPath;
            localDtdFileAsUrl = myLocalDtdPath.toURI().toURL();
        public InputSource resolveEntity (String publicId, String systemId) {
            if (systemId.equals( mySystemIdToIntercept )) {
                return new InputSource( localDtdFileAsUrl.toString() );
            else {
                // use the default behaviour (?)
                return null;
    LocalDTDResolver localDTDResolver =
    new LocalDTDResolver("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd";,
    new File("E:\\Tmp\\xhtml1-transitional.dtd")); // Place xhtml1-transitional.dtd in E:\
    SAXBuilder saxBuilder = new SAXBuilder(false);
    cityJDOMDocument = saxBuilder.build(new BufferedReader(new FileReader("C:\City.xml")));
However, it appears that the LocalDTDResolver class is partly working by generating the following different error:
java.io.FileNotFoundException: E:\Tmp\xhtml-lat1.ent (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:653)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1315)
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.startEntity(XMLEntityManager.java:1252)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.startPE(XMLDTDScannerImpl.java:722)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.skipSeparator(XMLDTDScannerImpl.java:2068)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDecls(XMLDTDScannerImpl.java:2031)
at com.sun.org.apache.xerces.internal.impl.XMLDTDScannerImpl.scanDTDExternalSubset(XMLDTDScannerImpl.java:320)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.dispatch(XMLDocumentScannerImpl.java:1202)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$DTDDriver.next(XMLDocumentScannerImpl.java:1090)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:1003)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:489)
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:888)
Do I need to put xhtml-lat1.ent (where from?) to E:\Tmp as well? And how to include it in LocalDTDResolver class as well?
Btw, is Apache xml-commons-resolver-1.2 needed to be included in the CLASSPATH for this solution to work?
Thanks again,
----- Original Message ----
From: Liam R E Quin <liam@w3.org>
To: Jack Bush <netbeansfan@yahoo.com.au>
Cc: xml-dev@lists.xml.org
Sent: Tue, 29 June, 2010 11:36:44 PM
Subject: Re: [xml-dev] How to open XML file that reference entity resolver (DTD)

On Tue, 2010-06-29 at 01:00 -0700, Jack Bush wrote:

> Looks like it is the default Sax parser possibly via JAXP that does
> not come with the XHTML DTDs and with an XML catalog set up to use
> them instead of going to www.w3.org each time.
> I am still waying through the references to use XML Catalog but it
> doesn't appears to provide every facets to a Java solution. It would
> great if you could provide some example of how everything are
> interconnected together.

When the XML parser sees it needs to fetch an external entity such as
the document type definition, it gets a SYSTEM identifier, and often (as
in this case) a PUBLIC identifier too.

There is a piece of code that maps the (PUBLIC, SYSTEM) pair into a data
stream. This code is usually called the entity resolver.

The default entity resolver will look up the SYSTEM and PUBLIC
identifiers in an external file, an XML Catalog, to see if they map
to a pre-configured local copy.

If not, the default entity resolver will treat the SYSTEM identifier as
a URI (relative to the actual document in which it occurred, just like a
link in an HTML document) and try to fetch it.

So you have several choices to avoid network access:
(1) add entries to the system XML Catalog
(2) add your own XML Catalog file
(3) override the default entity resolver
(4) edit the document.

> The 3rd option you have suggested by turning off validation with
> SAXBuilder.setValidating(false) does not prevent the process of
> resolving DTD document.
OK. Others have said that too, although I've seen it work -- you do
probably want the DTD loaded, though, with XHTML, so that entities work.

There are plenty of examples of options 1 to 3 floating around, using


Liam Quin - XML Activity Lead, W3C, http://www.w3.org/People/Quin/
Pictures from old books: http://fromoldbooks.org/
Ankh: irc.sorcery.net irc.gnome.org www.advogato.org


XML-DEV is a publicly archived, unmoderated list hosted by OASIS
to support XML implementation and development. To minimize
spam in the archives, you must subscribe before posting.

[Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
Or unsubscribe: xml-dev-unsubscribe@lists.xml.org
subscribe: xml-dev-subscribe@lists.xml.org
List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]

News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS