2 Replies Latest reply: Feb 24, 2008 12:00 PM by 807601 RSS

    catching a not well formed xml file

    807601
      I have a method that parses many xml files and after each one is processed they are entered into a mysql database. My problem is that when an xml file is found not to be well formed the input process into the mysql database stops.

      Thus, my question is: how do I get my program to stop trying to enter the failing file data into my mysql database and continue with the next one...at the same time that it tells me the name of the file that is causing the problem (this second part has been solved). My parsing method looks as follows:
      private Document parseXmlFile(String _filename, boolean _validating){
                try{
                     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                     factory.setValidating(_validating);
                     Document doc = factory.newDocumentBuilder().parse(new File(_filename));
                     return doc;
                } catch(SAXException e){
                     System.out.println("Bad XML");
                }
                catch(ParserConfigurationException parserconfigurationexception){     
                }
                catch(IOException ioexception){
                     System.out.println("Could not read source file: " + ioexception.getMessage());
                }
                return null;
           }
      then, the method that calls for this method and the xml data extraction method (which also works) is as follows:
      private void addFile(String _xmlPath){
                Document xmlFile = parseXmlFile(_xmlPath, false);
                addMyDoc(xmlFile);
           }
        • 1. Re: catching a not well formed xml file
          807601
          Your call the "parse" method will pick-up if the XML is not well-formed and so throw an exception. Just return null from this point...
                    try{
                         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                         factory.setValidating(_validating);
                         Document doc = factory.newDocumentBuilder().parse(new File(_filename));
                         return doc;
                    } catch(SAXException e){
                         System.out.println("Bad XML");
                                  // Return null if XML not well-formed.
                                  return null;
                    }
          Then check that "xmlFile is not null before attempting to write it in your "addFile" method.

          {code}
          private void addFile(String _xmlPath)
          {
                    Document xmlFile = parseXmlFile(_xmlPath, false);

          // Just check it's not null.
                    if (xmlFile != null)
          {
          addMyDoc(xmlFile);
          }
          }
          {code}
          • 2. Re: catching a not well formed xml file
            807601
            Thanks! that worked like a charm. I have one more question though. I know my program works because I have tested it every step of the way. However, I never contemplated that it could fail because of the a memory error. The program crashes randomly with a certain number of documents.

            My program is suppose to handle about 3,000 documents at a time. And it does, if I break it up the parsing from the mysql input. If I place both pieces together, I get the following error that I don't even know how to begin to fix or diagnose (I am using eclipse for lack of experience running java on the command line):
            Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
                 at java.util.Vector.<init>(Vector.java:111)
                 at java.util.Vector.<init>(Vector.java:124)
                 at java.util.Vector.<init>(Vector.java:133)
                 at org.apache.xerces.dom.DeepNodeListImpl.<init>(Unknown Source)
                 at org.apache.xerces.dom.ElementImpl.getElementsByTagName(Unknown Source)
                 at form4Code.Form4Db.addDerTranLine(Form4Db.java:1072)
                 at form4Code.Form4Db.addFile(Form4Db.java:53)
                 at form4Code.Form4Db.loadFiles(Form4Db.java:44)
                 at form4Code.Form4Db.run(Form4Db.java:36)
                 at form4Code.Form4Db.setDirectory(Form4Db.java:32)
                 at form4Code.FindXml.setDirectory(FindXml.java:28)
                 at form4Code.FindXml.<init>(FindXml.java:10)
                 at form4Code.Run.go(Run.java:36)
                 at form4Code.InsiderPanel$2.actionPerformed(InsiderPanel.java:133)
                 at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
                 at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
                 at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
                 at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
                 at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
                 at java.awt.Component.processMouseEvent(Component.java:6038)
                 at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
                 at java.awt.Component.processEvent(Component.java:5803)
                 at java.awt.Container.processEvent(Container.java:2058)
                 at java.awt.Component.dispatchEventImpl(Component.java:4410)
                 at java.awt.Container.dispatchEventImpl(Container.java:2116)
                 at java.awt.Component.dispatchEvent(Component.java:4240)
                 at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
                 at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
                 at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
                 at java.awt.Container.dispatchEventImpl(Container.java:2102)
                 at java.awt.Window.dispatchEventImpl(Window.java:2429)
                 at java.awt.Component.dispatchEvent(Component.java:4240)
            Edited by: jsidgman on Feb 24, 2008 9:59 AM