Releasing handle on XML file
Hi,
I need to know how do you release a handle on an XML file after parsing it. I am using the JAXP API for XML processing, in a Java servlet running on Java Web Server. I am creating the XML document object through the DocumentBuilderFactory, DocumentBuilder and Document interfaces.
After parsing the document, I am setting the interface object references to null, but the file is still not getting released and gives a sharing violation message when trying to open it in an editor. However, after shutting down Java Web Server, the XML file can be opened for editing.
I'd like to know any way to release the handle on the XML file in the servlet itself, rather than shutting down the web server each time for editing the file.
This is the code:
doPost(.........)
{
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try
{
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException pce)
{
System.err.println(pce);
System.exit(1);
}
// Step 3: parse the input file
Document doc = null;
try
{
doc = db.parse("../person.xml");
}
catch (SAXException se)
{
System.err.println(se.getMessage());
System.exit(1);
}
catch (IOException ioe)
{
System.err.println(ioe);
System.exit(1);
}
/* XML processing here */
doc = null;
db=null;
dbf=null;
}
Thanks in advance,
Jatin Shroff
[1588 byte] By [
jcshroff] at [2007-9-26 3:14:20]

Wow me too... This is reeeeeeeallly annoying.
I need to rename some files from a certain directory when I had a certain xml parsing error. Typically, this error occurs because these files are NOT xml (the users have put them there, nothing more I can do :P), so I want to clean them up right after parsing. But the **** sax parser keeps a handle on the files, and it tells me it cant rename them.
Please help, this is a very annoying behavior, and I havent found any workaround.
Are you passing the filename to the parser? If so, you have no hope of closing the file. If you pass a FileReader or some similar java.io object instead, you could try calling its close() method. Don't know if this would work, but I think it has a better than 50% chance.
I had noticed that my old version of Xerces didn't close its output file, but I'm surprised that current parsers still have that problem.
DrClap at 2007-6-29 11:24:29 >

private void ReadXML(ProcessedFile pfile) throws XMLFileProcessException {
DocumentBuilder docBuilder = null;
Document doc = null;
Exception err = null;
FileInputStream ins = null;
try {
ins = new FileInputStream(pfile.file);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilder = docBuilderFactory.newDocumentBuilder();
doc = docBuilder.parse(ins);
// parsing xml here....
} catch (SAXParseException spe) {
err = spe;
} catch (SAXException e) {
err = e;
} catch (java.io.IOException ioe) {
err = ioe;
} catch (javax.xml.parsers.ParserConfigurationException pce) {
err = pce;
} finally {
if( ins != null ) {
try {
ins.close();
} catch( java.io.IOException ioe ) {
String msg = "** Could not close file stream in finally clause for file " + pfile.file.getPath();
logger.severe(msg);
throw new XMLFileProcessException(msg, ioe);
}
}
ins = null;
doc = null;
docBuilder = null;
if( err != null )
throw new XMLFileProcessException(err);
}
}
The XMLFileProcessException is there because I need to do the same kind of cleanup for any kind of error I might get on a given file, so I wanted to have only one catch clause in the function who calls this one to do my cleanup code. And I noticed that if I throw this exception right away in my catch clauses in this function, the finally statement doesnt get executed, to my surprise. I also went a little paranoid with nulling all my variables there :)
Any better way to do this?