Validate an XML File against an XSD File – 根据XSD文件验证一个XML文件

最后修改: 2022年 7月 16日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this tutorial, we’ll demonstrate how to validate an XML file against an XSD file.

在本教程中,我们将演示如何根据XSD文件验证XML文件。

2. Definition of an XML and Two XSD Files

2.一个XML和两个XSD文件的定义

Let’s consider the following XML file baeldung.xml, which contains a name and an address, itself constituted of a zip code and a city:

让我们考虑以下XML文件baeldung.xml,它包含一个名字和一个地址,本身由一个邮政编码和一个城市构成。

<?xml version="1.0" encoding="UTF-8" ?>
<individual>
    <name>Baeldung</name>
    <address>
        <zip>00001</zip>
        <city>New York</city>
    </address>
</individual>

The content of baeldung.xml matches exactly the description of the person.xsd file:

baeldung.xml的内容与person.xsd文件的描述完全匹配。

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="individual">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string" />
                <xs:element name="address">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="zip" type="xs:positiveInteger" />
                            <xs:element name="city" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

However, our XML is not valid regarding the following XSD file full-person.xsd:

然而,我们的XML关于以下XSD文件是无效的full-person.xsd

<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="individual">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="5" />
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>
                <xs:element name="address">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="zip" type="xs:positiveInteger" />
                            <xs:element name="city" type="xs:string" />
                            <xs:element name="street" type="xs:string" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

There are two issues:

有两个问题。

  • The name attribute is limited to 5 characters maximum
  • The address expects a street attribute

Let’s see how we can use Java to obtain this information.

让我们看看如何使用Java来获得这些信息。

3. Validating an XML File Against an XSD File

3.根据XSD文件验证一个XML文件

The javax.xml.validation package defines an API for the validation of XML documents.

javax.xml.validation包定义了一个用于验证XML文档的API。

First, we’ll prepare a SchemaFactory capable of reading files that follow the XML Schema 1.0 specification. Then, we’ll use this SchemaFactory to create the Schema corresponding to our XSD file. A Schema represents a set of constraints.

首先,我们将准备一个SchemaFactory,它能够读取遵循XML Schema 1.0规范的文件。然后,我们将使用这个SchemaFactory来创建与我们的XSD文件对应的Schema。一个Schema代表一组约束条件。

Lastly, we’ll retrieve the Validator from the Schema. A Validator is a processor that checks an XML document against a Schema:

最后,我们将从Schema中检索ValidatorValidator是一个处理器,它根据Schema检查一个XML文档。

private Validator initValidator(String xsdPath) throws SAXException {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Source schemaFile = new StreamSource(getFile(xsdPath));
    Schema schema = factory.newSchema(schemaFile);
    return schema.newValidator();
}

In this code, the getFile method allows us to read the XSD into a File. In our example, we’ll put the file under the resources directory, so this method reads:

在这段代码中,getFile方法允许我们将读取XSDFile。在我们的例子中,我们将把文件放在资源目录下,所以这个方法会读取。

private File getFile(String location) {
    return new File(getClass().getClassLoader().getResource(location).getFile());
}

Let’s note that when we create the Schema, a SAXException can be thrown if the XSD file is not valid.

让我们注意,当我们创建Schema时,如果XSD文件无效,就会抛出一个SAXException

We can now use the Validator to validate that the XML file matches the XSD description. The validate method requires us to transform the File into a StreamSource:

我们现在可以使用Validator来验证XML文件是否符合XSD描述。validate方法要求我们将File转换成StreamSource

public boolean isValid(String xsdPath, String xmlPath) throws IOException, SAXException {
    Validator validator = initValidator(xsdPath);
    try {
        validator.validate(new StreamSource(getFile(xmlPath)));
        return true;
    } catch (SAXException e) {
        return false;
    }
}

The validate method throws a SAXException if there is an error during the parsing. This indicates that the XML file is not valid given the XSD specification.

如果在解析过程中出现错误,validate方法会抛出一个SAXException。这表明鉴于XSD规范,XML文件是无效的。

The validate method can also throw an IOException if there is an underlying problem while reading the File.

validate方法也可以抛出IOException,如果在读取文件时有潜在问题。

We can now wrap up the code in an XmlValidator class and check that baeldung.xml matches the person.xsd description but not full-person.xsd:

我们现在可以把代码封装在一个XmlValidator类中,并检查baeldung.xml是否符合person.xsd的描述,而不是full-person.xsd

@Test
public void givenValidXML_WhenIsValid_ThenTrue() throws IOException, SAXException {
    assertTrue(new XmlValidator().isValid("person.xsd", "baeldung.xml"));
}

@Test
public void givenInvalidXML_WhenIsValid_ThenFalse() throws IOException, SAXException {
    assertFalse(new XmlValidator().isValid("full-person.xsd", "baeldung.xml"));
}

4. Listing All Validation Errors

4.列出所有验证错误

The basic behavior of the validate method is to exit once the parsing throws a SAXException.

validate方法的基本行为是,一旦解析抛出一个SAXException就退出。

Now that we want to gather all validation errors, we need to change this behavior. For this, we have to define our own ErrorHandler:

现在我们想收集所有的验证错误,我们需要改变这个行为。为此,我们必须定义我们自己的ErrorHandler

public class XmlErrorHandler implements ErrorHandler {

    private List<SAXParseException> exceptions;

    public XmlErrorHandler() {
        this.exceptions = new ArrayList<>();
    }

    public List<SAXParseException> getExceptions() {
        return exceptions;
    }

    @Override
    public void warning(SAXParseException exception) {
        exceptions.add(exception);
    }

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

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

We can now tell the Validator to use this specific ErrorHandler:

我们现在可以告诉Validator来使用这个特定的ErrorHandler

public List<SAXParseException> listParsingExceptions(String xsdPath, String xmlPath) throws IOException, SAXException {
    XmlErrorHandler xsdErrorHandler = new XmlErrorHandler();
    Validator validator = initValidator(xsdPath);
    validator.setErrorHandler(xsdErrorHandler);
    try {
        validator.validate(new StreamSource(getFile(xmlPath)));
    } catch (SAXParseException e) 
    {
        // ...
    }
    xsdErrorHandler.getExceptions().forEach(e -> LOGGER.info(e.getMessage()));
    return xsdErrorHandler.getExceptions();
}

Since baeldung.xml meets the requirements of person.xsd, no error is listed in this case. However, calling with full-person.xsd, we will print the following error messages:

由于baeldung.xml符合person.xsd的要求,在这种情况下没有列出错误。然而,用full-person.xsd调用,我们将打印出以下错误信息。

XmlValidator - cvc-maxLength-valid: Value 'Baeldung' with length = '8' is not facet-valid with respect to maxLength '5' for type '#AnonType_nameindividual'.
XmlValidator - cvc-type.3.1.3: The value 'Baeldung' of element 'name' is not valid.
XmlValidator - cvc-complex-type.2.4.b: The content of element 'address' is not complete. One of '{street}' is expected.

All the errors we mentioned in section 1. were found by the program.

我们在第1节中提到的所有错误都被程序发现。

5. Conclusion

5.总结

In this article, we’ve seen how to validate an XML file against an XSD file and that we can also list all validation errors.

在这篇文章中,我们已经看到了如何根据XSD文件验证一个XML文件,而且我们还可以列出所有的验证错误。

As always, the code is available over on GitHub.

像往常一样,代码可在GitHub上获得