- Sản phẩm & Dịch vụSản phẩm & Dịch vụ
- Giải phápGiải pháp
- Bảng giáBảng giá
- Công tyCông ty
- Tài liệuTài liệu
vi
vi
Chuyên mục với các bài viết hướng dẫn, nghiên cứu và phân tích chi tiết về kỹ thuật, các xu hướng công nghệ mới nhất dành cho lập trình viên.
Web Developer
I’m passionate about web development and sharing my insights through articles, with over 8 years of experience. I hope these sharings inspire you and help build a strong web development community. @#@ Tôi đam mê phát triển web và chia sẻ những hiểu biết của mình thông qua các bài viết, với hơn 8 năm kinh nghiệm. Tôi hy vọng những chia sẻ này sẽ truyền cảm hứng cho các bạn và giúp xây dựng một cộng đồng phát triển web mạnh mẽ.
Thảo luận (0)
Đăng nhập để thảo luận
Java XML Validation API có thể được sử dụng để kiểm tra tính hợp lệ của XML so với XSD trong chương trình Java. Lớp javax.xml.validation.Validator được dùng trong chương trình này để kiểm tra XML với XSD trong Java.

Dưới đây là các tệp XSD và XML mẫu được sử dụng: 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>
Lưu ý rằng XSD trên có chứa hai phần tử gốc và cả không gian tên (namespace), tôi đã tạo hai tệp XML mẫu từ XSD này bằng công cụ Eclipse. 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>
Dưới đây là một tệp XML khác không tuân theo cấu trúc của Employee.xsd. employee.xml
<?xml version="1.0"?> <Employee> <name>Pankaj</name> <age>29</age> <role>Java Developer</role> <gender>Male</gender> </Employee>
Đây là chương trình được sử dụng để kiểm tra cả ba tệp XML so với XSD. Phương thức validateXMLSchema nhận tệp XSD và XML làm tham số và trả về true nếu xác nhận thành công, ngược lại trả về 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; }}
Kết quả đầu ra của chương trình trên là:
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
Lợi ích của việc sử dụng Java XML Validation API là chúng ta không cần phải tự phân tích (parse) nội dung tệp XML, và cũng không cần dùng đến bất kì APIs của bên thứ ba nào.