LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2009, 03:31 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
java xml parser keeps file locked after SAXException


Part of my program contains the below code to parse an xml file using standard java classes. My interest is in the blue part at the end where the code is doing the actual parsing.

Please note that I'm quite new to java and OOP, so there might be something very obvious that I have overlooked.
Code:
        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.

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


PS The editor that I use for editing the file tells me that the file is in use when I try to save the file.

Last edited by Wim Sturkenboom; 11-12-2009 at 03:32 AM.
 
Old 11-13-2009, 06:52 AM   #2
gzunk
Member
 
Registered: Sep 2006
Posts: 89

Rep: Reputation: 20
Not sure if this is related to your problem, but it would explain it:

http://bugs.sun.com/bugdatabase/view...bug_id=6359560

Essentially, Windows keeps a lock on memory mapped files even after you have closed them. Which makes memory mapped files useless on Windows. It's feasible that the XML parser implementation that you're using uses memory mapped files to read the file in.

You could try using a different XML parser implementation and seeing if that works.
 
Old 11-13-2009, 10:27 PM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks for the reply. I'm not 100% sure if it's the same (kind of) problem. I think that in that case windows would also keep the lock when a file is succesfully parsed.

I have posted the question on the sun forums as well and will see what will happen.
 
Old 02-24-2011, 09:32 AM   #4
inthemill
LQ Newbie
 
Registered: Feb 2011
Distribution: Ubuntu
Posts: 1

Rep: Reputation: Disabled
Just in case some one, like me, finds this thread searching for an answer to this question.
Here is my solution:

instead of
Code:
        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;
        }
write
Code:
        InputStream stream = null;
        try {
            stream = new FileInputStream(chooser.getSelectedFile());
            document = parser.parse(stream);
        }
        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;
        }finally{
            if(stream != null){
                stream.close();
            }
        }
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
checking for XML::Parser... configure: error: XML::Parser perl module is required for kornerr Linux - General 11 11-16-2008 07:24 AM
XML file parser using C language gopi_raghu Programming 2 08-22-2007 01:06 AM
Can't locate XML/Parser.pm ? plesaleza Programming 3 08-16-2007 01:16 AM
XML Parser... Prada Linux - Software 2 04-17-2007 11:26 AM
XML Parser bulkman Linux - Software 6 04-29-2005 07:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration