How to create XML validator(验证器;验证程序) from XML schema
In order to check XML data for validity we have to prepare its schema XSD-file. This file will be loaded by a JAXP package to a Schema objects instance. Then we'll use Schema to produce Validator which can then be used to validate any document with type defined in outr schema.
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; /**
* A sample application which shows how to perform a XML document validation.
*/ public class Test {
public static void main(String[] args) {
try {
// define the type of schema - we use W3C:
String schemaLang = "http://www.w3.org/2001/XMLSchema"; // get validation driver:
SchemaFactory factory = SchemaFactory.newInstance(schemaLang); // create schema by reading it from an XSD file:
Schema schema = factory.newSchema(new StreamSource("contacts.xsd"));
Validator validator = schema.newValidator(); // at last perform validation:
validator.validate(new StreamSource("contacts.xml")); } catch (SAXException ex) {
// we are here if the document is not valid:
System.out.println(ex.getMessage());
// ... process validation error...
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
How to create XML validator(验证器;验证程序) from XML schema的更多相关文章
- Flex 内置验证器—验证用户输入
今晚对于Flex中的Validator类(所有验证器的父类)测试一下 ---->其中常用的验证类有StringValidator,NumberValidator,DateValidator 测试 ...
- ThinkPHP验证器验证规则编码要点
首先验证器要继承框架的think\Validate类. 1.验证规则是一个父类的rule属性,是一个数组. 2.数组的键名是验证字段标识,值是验证规则.多个验证规则要用|分隔,不能有空格,否则可能会验 ...
- 9、 Struts2验证(声明式验证、自定义验证器)
1. 什么是Struts2 验证器 一个健壮的 web 应用程序必须确保用户输入是合法.有效的. Struts2 的输入验证 基于 XWork Validation Framework 的声明式验证: ...
- 实现一个可配置的java web 参数验证器
当使用java web的servlet来做接口的时候,如果严格一点,经常会对请求参数做一些验证并返回错误码.我发现通常参数验证的代码就在servlet里边,如果参数不正确就返回相应的错误码.如果接口数 ...
- struts2官方 中文教程 系列十一:使用XML进行表单验证
在本教程中,我们将讨论如何使用Struts 2的XML验证方法来验证表单字段中用户的输入.在前面的教程中,我们讨论了在Action类中使用validate方法验证用户的输入.使用单独的XML验证文件让 ...
- 自己定义验证器——用Struts2框架以框架师的思维灵活做好该事情
面对的问题:自己定义一个18位身份验证器.编写验证器.在validators.xml文件里进行注冊.在验证配置文件里使用? 第一部分:理解Struts2中自带的验证器 第二部分:如何通过server( ...
- struts2验证器
1. Struts2 的验证(note6,struts2-8) 1). 验证分为两种: > 声明式验证* >> 对哪个 Action 或 Model 的那个字段进行验证 >&g ...
- vue-validator(vue验证器)
官方文档:http://vuejs.github.io/vue-validator/zh-cn/index.html github项目地址:https://github.com/vuejs/vue-v ...
- 5款最好的免费在线网站CSS验证器
这里是一个名单, 5免费在线CSS验证器的网站.这些网站让你验证你的CSS代码的自由,没有任何麻烦.你可以选择上传文件,验证CSS添加URL,或简单的复制和粘贴完整的CSS代码.好的方面是,这些网站不 ...
- Ripple(瑞波币)validator-keys-tool 配置验证器
目录 Ripple(瑞波币)validator-keys-tool配置验证器 验证器密钥工具指南 验证器密钥 验证器令牌(Validator Keys) public_key撤销 签名 Ripple( ...
随机推荐
- (转载)delphi中获取汉字的拼音首字母
delphi中获取汉字的拼音首字母1.py: array[216..247] of string = ({216}'CJWGNSPGCGNESYPB' + 'TYYZDXYKYGTDJNMJ' + ' ...
- Omnithreadlibary学习(1)-异步执行
program main; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils, OtlParallel; begin Parallel.Async( ...
- python学习笔记enumerate()与range(len)运用及赋值小计
#!/uer/bin/env python # _*_ coding: utf-8 _*_ #格式1 a = 'abc' for i in range(len(a)): print a[i],'(%d ...
- C语言-06复杂数据类型-01数组
01-数组 #include <stdio.h> int main() { // 使用注意 // 都是正确写法 //int ages[5] = {10 , 11, 12, 67, 56}; ...
- Solr4.8.0源码分析(27)之ImplicitDocRouter和CompositeIdRouter
同样在公司工作中发现了一个现象, 1.我用/solr/admin/collections?action=CREATE&name=collection&numShards=3&r ...
- String的一些细节
String 常量池问题 (1) 字符串常量的"+"号连接,在编译期字符串常量的值就确定下来, 拿"a" + 1来说,编译器优化后在class中就已经是a1. ...
- Word文献类型标志
文献类型标识:专著:M:论文集:C:报纸文章:W:期刊文章:J:学位论文:D:研究报告:R:各种未定类型的文献:Z文献类型标识:专著:M:论文集:C:报纸文章:W:期刊文章:J:学位论文:D:研究报告 ...
- [CC150] Find a line passing the most number of points
Problem: Given a two-dimensional graph with points on it, find a line which passes the most number o ...
- 超级 Ping 监测工具——为您的网络状态保驾护航
关于 Ping Ping 是一个网络命令,主要是用于确定本地主机是否能与另一台主机交换(发送与接收)数据.根据返回的信息,就可以推断 TCP/IP 参数是否设置得正确以及运行是否正常.正常情况下,Pi ...
- Mysql table ful
http://ourmysql.com/archives/1327 http://blog.csdn.net/kevon_sun/article/details/7967728 http://my.o ...