import java.io.IOException;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.parsers.ParserConfigurationException;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;

  //import com.edankert.SimpleErrorHandler;
 public abstract class Wellformed extends DocumentBuilder {

   public static void main(String[] args) {
         try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
             factory.setValidating(false);
             factory.setNamespaceAware(true);

               DocumentBuilder builder = factory.newDocumentBuilder();
               builder.setErrorHandler(new SimpleErrorHandler());

               builder.parse(new InputSource("contacts.xml"));
            } catch (ParserConfigurationException e) {
               e.printStackTrace();
            } catch (SAXException e) {
               e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
          }

        }
      }

    import org.xml.sax.ErrorHandler;
    import org.xml.sax.SAXParseException;

      public class SimpleErrorHandler implements ErrorHandler {

       public void warning(SAXParseException e) {
      }

          public void error(SAXParseException e) {
           System.out.println(e.getMessage());
         }

          public void fatalError(SAXParseException e) {
           System.out.println(e.getMessage());
         }
     }

I am writing a java program for parsing an XML file and detecting if its UTF-8 compliant. I've gotten that part working but when it catches the first exception (syntax error) it stops the execution. I want it to find all the errors and not just the first one. Is there any way I can log the errors and not stop it on the first one? Here's my code.

That may not be such a good idea. Most of the errors that can be caught (missing > or <, mis-spelled keywords, unmatched start/end delimiters etc) will make it impossible to parse the remainder of the file anyway. All you will get is a load of spurious messages.

Is there anyway I can just log the first error in a file and negelect that line and start parsing after that line. and so on.. on every error?

The problem could be where to restart the scan. If each line is independent of all other lines, then it might be possible to start the scan on the next line. But if there is nesting of statements, there could be lots of errors as James said.

As a student you should try it and see what happens. Then come back and lets us know what you found. We all could learn something.

I was just wonering if thats possible considering that there's not nesting. If so what all functions/classes could be used

If the lines are all independent of each other, read each line into a buffer and parse it by itself.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.