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();

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

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


    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) {

          public void fatalError(SAXParseException e) {

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.

4 Years
Discussion Span
Last Post by NormR1

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.