简体   繁体   中英

How to validate xml against xsd and get *ALL* errors?

I have a standard code like below to validate xml against xsd, but it throw exception on first error and stops. How to validate xml, but continue on the first and next errors and get them all at the end ? Is it even possible ?

public static void validate(File xml, InputStream xsd) {
    try {
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        StreamSource xmlFile = new StreamSource(xml);
        validator.validate(xmlFile);

    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Between Validator validator = schema.newValidator(); and StreamSource xmlFile = new StreamSource(xml); add this fragment:

  final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
  validator.setErrorHandler(new ErrorHandler()
  {
    @Override
    public void warning(SAXParseException exception) throws SAXException
    {
      exceptions.add(exception);
    }

    @Override
    public void fatalError(SAXParseException exception) throws SAXException
    {
      exceptions.add(exception);
    }

    @Override
    public void error(SAXParseException exception) throws SAXException
    {
      exceptions.add(exception);
    }
  });

This way, after validate() you'll get full list of exceptions, but if one fatal error occurs, the parsing stops...

EDIT: the JavaDoc says: The application must assume that the document is unusable after the parser has invoked this method, and should continue (if at all) only for the sake of collecting additional error messages: in fact, SAX parsers are free to stop reporting any other events once this method has been invoked. So fatalError() may or may not cause the parsing to stop.

thanks for the code. The part which i am missing is where the errors are printed out to the console? Right now nothing happens?!

This is what i got:

        import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.*;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
//import java.io.File; // if you use File
import java.io.IOException;

public class ADT_XML_Validator {

    public static void main(String xml_pfad) throws IOException {




            try {
                SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
                Schema schema = factory.newSchema(new StreamSource("ADT_GEKID_v2.0.0.xsd"));
                Validator validator = schema.newValidator();

                final List<SAXParseException> exceptions = new LinkedList<SAXParseException>();
                validator.setErrorHandler(new ErrorHandler()
                {
                  @Override
                  public void warning(SAXParseException exception) throws SAXException
                  {

                    exceptions.add(exception);
                  }

                  @Override
                  public void fatalError(SAXParseException exception) throws SAXException
                  {
                    exceptions.add(exception);
                  }

                  @Override
                  public void error(SAXParseException exception) throws SAXException
                  {
                    exceptions.add(exception);
                  }
                });


                StreamSource xmlFile = new StreamSource(xml_pfad);
                validator.validate(xmlFile);


            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }



    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM