This discussion is archived
1 2 Previous Next 21 Replies Latest reply: Nov 16, 2009 10:35 AM by 796447 RSS

java xml parser keeps file locked after SAXException

807580 Newbie
Currently Being Moderated
Totally new to Java and OOP so there might be something very obvious to solve the problem. I have the following code as part of an XML viewer; the second code block below shows the code that cause the problem and the first code block is added for completeness. In the source they are consecutive.
        DocumentBuilderFactory factory;
        DocumentBuilder parser;
        Document document;

        String Error;

        // setup the XML parser
        try {
            factory = DocumentBuilderFactory.newInstance();
        }
        catch (FactoryConfigurationError eFactoryConfigurationError) {
            Error = "XML\n" + eFactoryConfigurationError;
            JOptionPane.showMessageDialog(null, Error, "Software Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        factory.setIgnoringComments(false); /* get comments */
        factory.setCoalescing(false);       /* get separate entries for cdata, text etc */
        factory.setNamespaceAware(false);
        factory.setValidating(false);

        try {
            // prevent the use of a possible DTD
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

            parser = factory.newDocumentBuilder();
        }
        catch (ParserConfigurationException eParserConfigurationException) {
            Error = "XML\n" + eParserConfigurationException;
            JOptionPane.showMessageDialog(null, Error, "Software Error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        // parse the file
        try {
            document = parser.parse(chooser.getSelectedFile());
        }
        catch (SAXException eSAXException) {
            Error = eSAXException.getMessage();
            JOptionPane.showMessageDialog(null, Error, "XML error", JOptionPane.ERROR_MESSAGE);
            return;
        }
        catch (IOException eIOException) {
            Error = "" + eIOException.getMessage();
            JOptionPane.showMessageDialog(null, Error, "File error", JOptionPane.ERROR_MESSAGE);
            return;
        }
If an xml file (indicated by chooser.getSelectedFile()) contains an error, a SAXException is thrown.

When I run the program in WindowsXP, I've found that the xml file is locked for writes by the parser and the lock is not released when the error occurs. At this stage I must close the java application to clear the lock. The problem does not occur if no SAXException is thrown.

So I'm looking for a way to remove the lock. Any advice?


PS 1)
I use an editor (PFE, very old) to edit the xml file and next reload it in the java application that contains the above code. The editor tells me that the file is in use when it tries to rename the file (as part of a save operation) after the exception is thrown (and even crashes after that, but that is something else).
PS 2)
I actually consider this a bug as the lock is released if there are no errors; I would expect the lock behaviour to be identical in an error and a none error situation; but I might be wrong here.

Edited by: WimS on Nov 13, 2009 5:51 AM
  • 1. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    Locked by the parser? Or locked by an editor or some other process you're using to look at the file?
  • 2. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    Thanks for the reply.

    As far as I know the parser. The editor does not lock the file and allows other applications to modify the content of the file that it has open. And those are the only applications as far as I know that have the xml file open.

    Edited by: WimS on Nov 13, 2009 6:16 AM
  • 3. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    Little kick
  • 4. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    WimS wrote:
    Little kick
    You code
        document = parser.parse(chooser.getSelectedFile());
           
    parses the File. You could always open a FileInputStream and parse that and then explicitly close the FileInputStream in the event of an exception.
  • 5. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    Thanks for the reply.

    That's my current workaround; but the problem with that is that the document node in the xml is null in that case. So then I have to work around that as well.
  • 6. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    WimS wrote:
    Thanks for the reply.

    That's my current workaround; but the problem with that is that the document node in the xml is null in that case. So then I have to work around that as well.
    Why do you have to work around the document node being null? Presumable the parsing failed because the XML does not meet the XML spec in some way. Just sent the XML(ish) file back to the supplier and tell him/her to fix the problem.
  • 7. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    You might be missing the point slightly. I 'am' the supplier (you don't know that) and I want to be able to fix the XML file without having to close the application that is reading (and locking) it.

    PS The document node contains the filename that is being 'processed'. If I use a inputstream, I don't have that filename.

    Edited by: WimS on Nov 16, 2009 7:55 AM
    Adde PS
  • 8. Re: java xml parser keeps file locked after SAXException
    Tolls Journeyer
    Currently Being Moderated
    If the XML is incorrect then there is no way you'll get a document out, because the XML is incorrect.
    If you want to "repair" the document then all you can do is read it in as text and dig out the filename manually.
    Unless I'm missing something here.
  • 9. Re: java xml parser keeps file locked after SAXException
    DrClap Expert
    Currently Being Moderated
    Yes, you are missing something here. Namely, you're missing the whole point of the question. I've encountered the same problem (using software which I didn't write) and I agree that it's very annoying to have to stop the application and restart it just because I happened to feed it malformed XML.
  • 10. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    WimS wrote:
    You might be missing the point slightly. I 'am' the supplier (you don't know that) and I want to be able to fix the XML file without having to close the application that is reading (and locking) it.
    OK.

    >
    PS The document node contains the filename that is being 'processed'. If I use a inputstream, I don't have that filename.
    But you have the file name in the scope of the exception - you created the FileinputStream from it.

    >
    Edited by: WimS on Nov 16, 2009 7:55 AM
    Adde PS
  • 11. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    DrClap wrote:
    Yes, you are missing something here. Namely, you're missing the whole point of the question. I've encountered the same problem (using software which I didn't write) and I agree that it's very annoying to have to stop the application and restart it just because I happened to feed it malformed XML.
    Many years ago I worked on an document management system where documents were wrapped in an XML envelope and place in a Documentum repository. My colleague was working on the XML parsing and had to test everything on 100,000 odd documents. This was expected to take about 10 hours so the testing was left on overnight.

    After about 55,000 documents the whole system just hung with as near as dammit 100% CPU time. After 3 days the problem document was isolated and then a day later the problem was found. The MS Java XML parser had a feature whereby if the XML contained a byte of zero (it should never have) the parser never moved on to the next character so it looped forever.
  • 12. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    Tolls wrote:
    If the XML is incorrect then there is no way you'll get a document out, because the XML is incorrect.
    If you want to "repair" the document then all you can do is read it in as text and dig out the filename manually.
    Unless I'm missing something here.
    Yep, but with FileInputStream I also don't get it if the document is correct; it's still null.
  • 13. Re: java xml parser keeps file locked after SAXException
    796447 Newbie
    Currently Being Moderated
    WimS wrote:
    Tolls wrote:
    If the XML is incorrect then there is no way you'll get a document out, because the XML is incorrect.
    If you want to "repair" the document then all you can do is read it in as text and dig out the filename manually.
    Unless I'm missing something here.
    Yep, but with FileInputStream I also don't get it if the document is correct; it's still null.
    Then you're just simply getting the FileInputStream in the wrong way, by passing it the wrong path to the file for instance. This has nothing to do whatsoever whether the content of the file is correct or not.
  • 14. Re: java xml parser keeps file locked after SAXException
    807580 Newbie
    Currently Being Moderated
    sabre150 wrote:
    But you have the file name in the scope of the exception - you created the FileinputStream from it.
    After I've created the document using parser.parse, I recursively walk through the document using a method that I did not post in the code as I did not consider it not relevant at that time. As the document node is now null when I use the FileInputStream, I can not show the user the filename in the document node.

    That is my problem at this moment. I obviously can pass an additional parameter with the filename to the method and if I encounter a document node, insert it there. I will post the missing code tomorrow.
1 2 Previous Next