Re: [xml-dev] Problem parsing XML file with Xerces-J

From
Midsummer Sun <>
To
Michael Kay <>
Date
2005-04-01T04:20:25Z
ID
<>
Thread
Re: [xml-dev] Problem parsing XML file with Xerces-J
My class implements EntityResolver, and I define the below function in
my class..

public InputSource resolveEntity(java.lang.String publicId,
                                java.lang.String systemId)
{

   InputSource is = new InputSource();
   is.setSystemId("file:///C:/x.dtd");

   return is;
}

A example program in docs specify the resolveEntity method without
IOException and SAXException in the signature. But the formal
definition of this method show the signature as (i.e. with exception
declarations)
public InputSource resolveEntity(java.lang.String publicId,
                                 java.lang.String systemId)
                   throws SAXException,
                   java.io.IOException

The function definition without exception signatures is working for me!

And I "set my class object" as the EntityResolver handler "to the parser" as: 
builder.setEntityResolver(obj); //obj is the class object

In the above method I set the SystemId to a dummy DTD file. It is
totally blank (it is of 0 bytes). I just create a file x.dtd using
notepad "without any contents" and place it somewhere.. Thats it.

This works!

But is it possible that I don't create any redundant resource at all
(i.e. like x.dtd)  ? Some thing like - is.setSystemId("") or
is.setSystemId(null). But arguments "" and null are not working. I am
getting error java.net.MalformedURLException: no protocol: and
java.net.MalformedURLException respectively.

I was just thinking..
A function like resolveEntities(boolean) in the DocumentBuilder class
would be a useful addition to the API. If I specify a "true" argument
the callback method resolveEntity will be called otherwise not.
Also if I invoke the API resolveEntities(false) then the class does'nt
need to implement EntityResolver interface.


I have another observation:
I think pre-editing of response XML (i.e. stripping DTD declration) is
more better "for me". For my requirement, DTD in the XML is useless to
me. Implementing EntityResolver imposes significant performance
overhead to my program. The parser is always pooling for callback
events.. So I think pre-editing by a simple string method is far
efficient..

Best regards,

On Mar 31, 2005 7:32 PM, Michael Kay <> wrote:
> I doubt it will work, I think you have to supply a Reader that reads an
> empty document, e.g. inputSource.setReader(new StringReader("")). Check the
> spec.
> 
> Michael Kay
> http://www.saxonica.com/
> 
> > -----Original Message-----
> > From: Midsummer Sun [mailto:]
> > Sent: 31 March 2005 14:38
> > To: Michael Kay
> > Cc: 
> > Subject: Re: [xml-dev] Problem parsing XML file with Xerces-J
> >
> > I am sorry if I am breaching the protocol of the list. But can you
> > please tell whether just this method call "within resolveEntity method
> > (shown in my last post)"
> >
> > return new InputSource();
> >
> > shall achive "return an InputSource pointing to an empty DTD"
> > you suggested..
> > Or do I have to do something else?
> >
> > I'll try to solve the exception handling error myself..
> >
> > Best regards,
> >
> > On Thu, 31 Mar 2005 13:48:41 +0100, Michael Kay
> > <> wrote:
> > > I'm not the right person, and this isn't the right place,
> > to teach you Java
> > > programming.
> > >
> > > Michael Kay
> > > http://www.saxonica.com/
> > >
> > > > -----Original Message-----
> > > > From: Midsummer Sun [mailto:]
> > > > Sent: 31 March 2005 13:20
> > > > To: Michael Kay
> > > > Cc: 
> > > > Subject: Re: [xml-dev] Problem parsing XML file with Xerces-J
> > > >
> > > > I am obliged by your help.
> > > >
> > > > So I have done this change to my Java class (which extends
> > > > DefaultHandler) i.e. added this method definition..
> > > >
> > > > public InputSource resolveEntity(java.lang.String publicId,
> > > >                                    java.lang.String systemId)
> > > >                      throws java.io.IOException, SAXException
> > > >  {
> > > >
> > > >     return new InputSource();
> > > >  }
> > > >
> > > > I guess that "return new InputSource()" method call will return an
> > > > InputSource pointing to an empty DTD(you suggested this
> > workaround).
> > > > Please correct me if I am wrong.
> > > >
> > > > But the program is giving error:
> > > >
> > > > XYZ.java:125:
> > resolveEntity(java.lang.String,java.lang.String) in XYZ
> > > > cannot override
> > resolveEntity(java.lang.String,java.lang.String) in
> > > > org.xml.
> > > > sax.helpers.DefaultHandler; overridden method does not throw
> > > > java.io.IOException
> > > >
> > > >   public InputSource resolveEntity(java.lang.String publicId,
> > > >                      ^
> > > > 1 error
> > > >
> > > > Now this seems to be a Java exception handling problem..
> > I am good at
> > > > Java but not too good!
> > > >
> > > > Please tell the remedy.
> > > >
> > > > Best regards,
> > > >
> > > >
> > > > On Thu, 31 Mar 2005 12:26:54 +0100, Michael Kay
> > > > <> wrote:
> > > > > You need to do two things
> > > > >
> > > > > (a) register your EntityResolver with the parser -
> > you've done this
> > > > >
> > > > > (b) use your EntityResolver to resolve URI references - you
> > > > seem to have
> > > > > omitted this step.
> > > > >
> > > > > This means you need to implement the resolveEntity() method
> > > > to do something
> > > > > useful when presented with the system ID of the
> > external DTD - like
> > > > > returning an InputSource pointing to an empty DTD.
> > > > >
> > > > > The default implementation of resolveEntity() in
> > DefaultHandler does
> > > > > nothing, you need to override it.
> > > > >
> > > > > Michael Kay
> > > > > http://www.saxonica.com/
> > > >
> > >
> > >
> >
> 
> -----------------------------------------------------------------
> The xml-dev list is sponsored by XML.org <http://www.xml.org>, an
> initiative of OASIS <http://www.oasis-open.org>
> 
> The list archives are at http://lists.xml.org/archives/xml-dev/
> 
> To subscribe or unsubscribe from this list use the subscription
> manager: <http://www.oasis-open.org/mlmanage/index.php>
> 
>