简体   繁体   中英

How to list all the errors after I validate xml against xsd?

I have searched online and tried for few days but I still can't list out all the errors, I only can show 1 error. Why? I need to list out all the error. Hope someone help me. Below is my validate and error handler code. The program goes to error() only, and only once. It error for the first element but there are other elements missing in the XML as well, it does not display them.

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class XMLValidate1 {

    public static void main(String[] args) throws SAXException, IOException {


        SchemaFactory factory 
         = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        File schemaLocation = new File("Q1.xsd");
        Schema schema = factory.newSchema(schemaLocation);
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new ValidateErrorHandler());      
        Source xmlFile = new StreamSource(new File("Q1.xml"));

        try {
            validator.validate(xmlFile);
            System.out.println(xmlFile.getSystemId() + " is valid.");
        }
        catch (SAXException ex) {
            System.out.println(xmlFile.getSystemId() + " is not valid because ");
            System.out.println(ex.getMessage());
        }  

    }

}

Below is my errorhandler

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

public class ValidateErrorHandler implements ErrorHandler {

    public void warning(SAXParseException ex) {
        System.out.println("Warning: "); 
        System.err.println(ex.getMessage());
    }

    public void error(SAXParseException ex) {
        System.out.println("Error: "); 
        System.err.println(ex.getMessage());
    }

    public void fatalError(SAXParseException ex) throws SAXException {
        System.out.println("Fatal error: "); 
         System.err.println(ex.getMessage());
    }

}

Firstly, this depends on which schema validator you are using. The JAXP interface is designed to work with a variety of schema validators. If you are using the default one that comes with the JDK, you should let us know.

All validators are going to be able to recover from some errors and not others, so the other thing we need to know is what the particular error is. It would be useful to post a specimen XSD and XML so we can compare results.

If you want to consider using Saxon as your schema validation engine, it has an option to give you a listing of all validation errors in an XML report, which you can format to your own requirements using XSLT.

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