Java XML Validation API를 사용하여 Java 프로그램에서 XML을 XSD에 대해 유효성을 검사할 수 있습니다. 이 프로그램에서 javax.xml.validation.Validator
클래스를 사용하여 Java에서 xml을 xsd에 대해 유효성을 검사합니다.
XML을 XSD에 대해 유효성 검사
여기에 사용된 샘플 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에는 두 개의 루트 요소와 네임스페이스가 포함되어 있고, 나는 이에 따라 이클립스를 사용하여 두 개의 샘플 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>
다음은 Employee.xsd
와 일치하지 않는 다른 XML 파일입니다. employee.xml
<?xml version="1.0"?>
<Employee>
<name>Pankaj</name>
<age>29</age>
<role>Java Developer</role>
<gender>Male</gender>
</Employee>
세 개의 XML 파일을 XSD에 대해 유효성을 검사하는 데 사용된 프로그램은 다음과 같습니다. 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를 사용하는 이점은 파일을 구문 분석할 필요가 없으며 제3자 API를 사용하지 않아도 된다는 것입니다.
Source:
https://www.digitalocean.com/community/tutorials/how-to-validate-xml-against-xsd-in-java