如何在 Java 中根据 XSD 验证 XML
Java XML 验证 API 可用于在 Java 程序中根据 XSD 验证 XML。javax.xml.validation.Validator
此程序中使用类来在 Java 中根据 xsd 验证 xml。
根据 XSD 验证 XML
以下是使用的示例 XSD 和 XML 文件。Employee.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="https://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.journaldev.com/Employee"
xmlns:empns="https://www.journaldev.com/Employee" elementFormDefault="qualified">
<element name="empRequest" type="empns:empRequest"></element>
<element name="empResponse" type="empns:empResponse"></element>
<complexType name="empRequest">
<sequence>
<element name="id" type="int"></element>
</sequence>
</complexType>
<complexType name="empResponse">
<sequence>
<element name="id" type="int"></element>
<element name="role" type="string"></element>
<element name="fullName" type="string"></element>
</sequence>
</complexType>
</schema>
请注意,上面的 XSD 还包含两个根元素和命名空间,我使用 Eclipse 从 XSD创建了两个示例XML 文件。EmployeeRequest.xml
<?xml version="1.0" encoding="UTF-8"?>
<empns:empRequest xmlns:empns="https://www.journaldev.com/Employee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.journaldev.com/Employee Employee.xsd ">
<empns:id>5</empns:id>
</empns:empRequest>
EmployeeResponse.xml
<?xml version="1.0" encoding="UTF-8"?>
<empns:empResponse xmlns:empns="https://www.journaldev.com/Employee" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://www.journaldev.com/Employee Employee.xsd ">
<empns:id>1</empns:id>
<empns:role>Developer</empns:role>
<empns:fullName>Pankaj Kumar</empns:fullName>
</empns:empResponse>
这是另一个不符合的 XML 文件Employee.xsd
。employee.xml
<?xml version="1.0"?>
<Employee>
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</Employee>
下面是用于根据 XSD 验证所有三个 XML 文件的程序。该validateXMLSchema
方法以 XSD 和 XML 文件作为参数,如果验证成功则返回true ,否则返回false。XMLValidation.java
package com.journaldev.xml;
import java.io.File;
import java.io.IOException;
import javax.xml.XMLConstants;
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 XMLValidation {
public static void main(String[] args) {
System.out.println("EmployeeRequest.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "EmployeeRequest.xml"));
System.out.println("EmployeeResponse.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "EmployeeResponse.xml"));
System.out.println("employee.xml validates against Employee.xsd? "+validateXMLSchema("Employee.xsd", "employee.xml"));
}
public static boolean validateXMLSchema(String xsdPath, String xmlPath){
try {
SchemaFactory factory =
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
validator.validate(new StreamSource(new File(xmlPath)));
} catch (IOException | SAXException e) {
System.out.println("Exception: "+e.getMessage());
return false;
}
return true;
}
}
上述程序的输出是:
EmployeeRequest.xml validates against Employee.xsd? true
EmployeeResponse.xml validates against Employee.xsd? true
Exception: cvc-elt.1: Cannot find the declaration of element 'Employee'.
employee.xml validates against Employee.xsd? false
使用 Java XML 验证 API 的好处是我们不需要解析文件,并且不需要使用第三方 API。