Is there no way to specify where to search for the DTD or XML Schema,
besides setting user.dir?

Thanks,

Rob

-----Original Message-----
From: Brad Buckingham [mailto:[EMAIL PROTECTED]
Sent: Wednesday, April 10, 2002 3:28 PM
To: [EMAIL PROTECTED]
Subject: Re: How to find non local grammer?


Doug,

If I understand your issue correctly, it
sounds like something I also encountered
a while back.

Assuming that your DTD and XML file are on
the same system, you may add the directory
location of your DTD to the CLASSPATH,
implement the EntityResolver class overriding
the resolveEntity() method to locate
the DTD using the CLASSPATH.  Then set
this as your parser's entity resolver
prior to parsing the XML document.

Below are a couple of code snippets:

Setting resolver:

DOMParser parser = new DOMParser();
parser.setEntityResolver(new STAEntityResolver());


Class that implements EntityResolver:

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.*;
import java.net.URL;
import java.util.HashMap;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.xerces.parsers.DOMParser;

//
//  Class Name: STAEntityResolver
//
//  Description:  This class will locate the entity (e.g. DTD or XML)
//  that has been encountered by searching the user's CLASSPATH.
//
public class STAEntityResolver implements EntityResolver
{
        /**
         * This attempts to resolve the entity associated with the specified
         * public and system ids. If the systemId is empty, then we use the
         * publicId to locate the URL of the cataloged DTD file.
         */
        public InputSource resolveEntity(String publicId, String systemId)
                throws SAXException, IOException {

                String dtd = getUrl(systemId);

                //
                // uses classpath to locate the dtd
                //
                return new InputSource(getClass().getResourceAsStream("/"+dtd));
        }

        /**
         * Return the URL of the DTD corresponding to the systemId.
         */
        private static final String getUrl(String systemId) {
                File file = new File(systemId);
                String name = file.getName();
                return name;
        }
}


Doug Helton wrote:
>
> UnFortunately no, I don't want it to overide the name of the schema or
dtd,
> What I realy need is to somehow be able to tell it where to look for that
> grammer.
>
> Thanks,
> Doug Helton
>
> -----Original Message-----
> From: Gottfried Szing [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, April 10, 2002 2:51 PM
> To: Xerces-J-User
> Subject: Re: How to find non local grammer?
>
> On Thu, 11 Apr 2002, Doug Helton wrote:
>
> > Hi,
> >
> >       I am using an InputSource as input into my DOMparser,
> > and trying to validate files with schemas and DTDs that are
> > not local to my xml file.  I can't specify a relative path in
> > my file because I may not know where my file will be located,
> > I am at a loss.  I tried setting the systemID on the
> > InputSource but that doesn't seem to be what I need to do or
> > I am somehow using it wrong.  Does anyone have any
> > suggestions?
>
> maybe the feature for external-schema locations help you?
>
> http://xml.apache.org/xerces2-j/features.html
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to