简体   繁体   English

在针对 xsd 验证 xml 后如何列出所有错误?

[英]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.我在网上搜索并尝试了几天,但我仍然无法列出所有错误,我只能显示 1 个错误。 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.程序只去 error() ,而且只去一次。 It error for the first element but there are other elements missing in the XML as well, it does not display them.第一个元素出错,但 XML 中也缺少其他元素,它不会显示它们。

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. JAXP 接口旨在与各种模式验证器一起使用。 If you are using the default one that comes with the JDK, you should let us know.如果您使用的是 JDK 附带的默认设置,您应该告诉我们。

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.发布样本 XSD 和 XML 会很有用,这样我们就可以比较结果。

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.如果您想考虑使用 Saxon 作为您的模式验证引擎,它可以选择在 XML 报告中为您提供所有验证错误的列表,您可以使用 XSLT 将其格式化为您自己的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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