Dave,

        I did not due my client application justice with the posting of my 
original
email, it was much too complicated to put in an email I greatly simplified
it in order to not cloud my real question.  My application index PI's stored
before the start element of the document, it takes those PI's in the form of
name value pairs and indexes them using Lucene.  Users can then search on
these PI's.  The PI's act as meta data for the XML file, clients can then
search on this meta data to locate XML files that contain information they
are looking for.  It is a lot faster than indexing an entire XML file.

        Okay now back to the real issue, as mentioned since I do care about the
content of the XML file I would like to abort parsing as soon as I encounter
the first element of the XML file.  I read on the net that by throwing a
SAXException parsing would be aborted and from what I can tell seems to
work, but you made a good point below, which does not support the many
claims that throwing an exception will abort parsing.  So what is the
correct way to abort parsing?

        The way the Handler I am using works is as follows, the constructor of 
the
handler creates the SAX parser and invokes the parse method, when the
handlers startElement method is called I throw an exception hoping to abort
parsing.  Later on I then try to delete that file (just testing, no other
reason) but I cannot.

public void startElement(String uri, String localName, String qName,
                         Attributes attributes) throws SAXException{
                throw new SAXException("Finished indexing document: " + uri);
    }

        In any case from what you are saying throwing an exception will not 
abort
the parser?  Which means if I wait a few seconds I should be able to access
the file?

Thanks,

Rob


-----Original Message-----
From: Dave Flanagan [mailto:[EMAIL PROTECTED]
Sent: Tuesday, August 12, 2003 11:28 PM
To: [EMAIL PROTECTED]
Subject: Re[2]: XMLReader not releasing file reference


Hello Rob,

In your original mesage Tuesday, August 12, 2003 11:15 AM you wrote
RO> ...Since I am only interested in Processing Instructions
RO> when I encounter the start element I throw a SAX Exception to abort
RO> parsing:

I am assuming you may have meant to say when you encounter the
Document Element which of course would be the first start element in
the file.
Which also leads me to believe you make the assumption ALL the processing
instructions you are concerned with exist solely in the prolog of the
XML document.
So you are apparently not interested in a processing instruction
that may occur within in an arbitrary position within the xml document
- which is fine if that is what you are trying to do.

I have run 1 meg files through Xerces 2.4.0 and experience no problem
in deleting the file when an exception is thrown during the parsing
process, but I think I may have an idea of where you are running
into the problem:

It depends on where in your application you are attempting to catch
the exception being generated and where you are attempting to delete
the file in question.
    If you are attempting to delete the file within the startElement
    method of the ContentHandler interface(which sounds like what you
    may be doing since you say you "immediately" attempt to delete the
    file) you have not yet returned from the parse method of the
    XMLReader.

    Since you have not yet returned from the parse method (Technically
    the XMLReader may have more reading it needs to perform - so it
    makes sense that it would maintain a reference to the file.

    The SAXException does not indicate the end of parsing but rather
    mereley that there has been an exception generated during the
    parsing.

You may want to try the following:
Surround the parse method call inside of a try block
and catch the exception at this level in your code


    File in = new File("sample.xml");
    String uri = "file:" + in.getAbsolutePath();
    SAXParserFactory   spf = SAXParserFactory.newInstance();
    SAXParser  sp = spf.newSAXParser();
    XMLReader  reader = sp.getXMLReader();
    try {
        reader.parse(uri);
    }
    catch(SAXException e)
    {
        // attempting to delete the file here should be ok
        // and makes sense since at this point parsing is
        // definately complete so the reader no longer maintains
        // a reference to the file in question
        in.delete();
    }

Hope this helps
Just makes sense to me not to try to delete the file until the code
has returned from the parse method.

Dave Flanagan



---------------------------------------------------------------------
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