博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何在Java中针对XSD验证XML
阅读量:2533 次
发布时间:2019-05-11

本文共 3265 字,大约阅读时间需要 10 分钟。

Java XML Validation API can be used to validate XML against XSD in java program. javax.xml.validation.Validator class is used in this program to validate xml against xsd in java.

Java XML验证API可用于针对Java程序中的XSD验证XML。 此程序中使用javax.xml.validation.Validator类针对Java中的xsd验证xml。

针对XSD验证XML (Validate XML against XSD)

Here are the sample XSD and XML files used.

这是使用的示例XSD和XML文件。

Employee.xsd

Employee.xsd

Notice that above XSD contains two root element and namespace also, I have created two sample .

注意,上面的XSD还包含两个根元素和名称空间,我已经创建了两个示例 。

EmployeeRequest.xml

EmployeeRequest.xml

5

EmployeeResponse.xml

EmployeeResponse.xml

1
Developer
Pankaj Kumar

Here is another XML file that doesn’t confirms to the Employee.xsd.

这是另一个未向Employee.xsd确认的XML文件。

employee.xml

employee.xml

Pankaj
29
Java Developer
Male

Here is the program that is used to validate all three XML files against the XSD. The validateXMLSchema method takes XSD and XML file as argument and return true if validation is successful or else returns false.

这是用于针对XSD验证所有三个XML文件的程序。 validateXMLSchema方法将XSD和XML文件作为参数,如果验证成功,则返回true ,否则返回false

XMLValidation.java

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;    }}

Output of the above program is:

上面程序的输出是:

EmployeeRequest.xml validates against Employee.xsd? trueEmployeeResponse.xml validates against Employee.xsd? trueException: cvc-elt.1: Cannot find the declaration of element 'Employee'.employee.xml validates against Employee.xsd? false

The benefit of using Java XML validation API is that we don’t need to parse the file and there is no third party APIs used.

使用Java XML验证API的好处是我们不需要解析文件,并且不使用任何第三方API。

翻译自:

转载地址:http://ioqzd.baihongyu.com/

你可能感兴趣的文章
阶段3 2.Spring_01.Spring框架简介_05.spring的优势
查看>>
阶段3 2.Spring_02.程序间耦合_7 分析工厂模式中的问题并改造
查看>>
阶段3 2.Spring_02.程序间耦合_4 曾经代码中的问题分析
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_2 spring中的Ioc前期准备
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_09.JdbcTemplate的基本使用_5 JdbcTemplate在spring的ioc中使用
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_02.ssm整合之搭建环境
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_3、快速创建SpringBoot应用之手工创建web应用...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_04.ssm整合之编写SpringMVC框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第1节零基础快速入门SpringBoot2.0_5、SpringBoot2.x的依赖默认Maven版本...
查看>>
阶段3 3.SpringMVC·_07.SSM整合案例_08.ssm整合之Spring整合MyBatis框架
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>