#4 DOM4J reading local file DTD

open
Maarten Coene
None
5
2012-10-08
2004-08-19
Larry Cryderman
No

My inbound XML documents reference a file dtd such
as '<!DOCTYPE Foo SYSTEM "Foo.dtd">....I can't obtain
a Document object from the SAXReader because it can't
resolve the URI. I've tried using an EntityResolver, but
it's never even referenced. Help plz!

Discussion

  • Maarten Coene
    Maarten Coene
    2004-08-22

    Logged In: YES
    user_id=178745

    Hi Larry,

    1. The reason the SAXReader can't resolve the URI is that
      you didn't specify a systemID in your InputSource. If you use
      dom4j, you don't have to create the InputSource yourself,
      but you can use one of these methods below which create
      the InputSource for you:

    org.dom4j.io.SAXReader.read(File)
    org.dom4j.io.SAXReader.read(InputStream, String)
    org.dom4j.io.SAXReader.read(Reader, String)
    org.dom4j.io.SAXReader.read(URL)

    Please use one of these methods for creating your Document.
    If you use the read(File) or read(URL) method, the DTD has
    to be located in the same directory as your XML document. If
    you use the other methods, you'll have to provide the
    systemID yourself.

    1. The reason that your EntityResolver is not used is hard to
      say if you don't provide your source code. However, some
      SAX Parser don't use the EntityResolver in some situations,
      even if you have specified one. Perhaps you can try to use
      the Xerces XML parser, which is behaving correctly in this
      situation. You can do this like this:
      org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader
      ("org.apache.xerces.parsers.SAXParser");

    Did this solve your problems?

    regards,
    Maarten

     
  • Paul Libbrecht
    Paul Libbrecht
    2006-06-19

    Logged In: YES
    user_id=647450

    have you try using new SAXReader().read(file.toURL()) ?
    I suspect this would solve your issues.
    paul