码迷,mamicode.com
首页 > 编程语言 > 详细

Java SAX Schema Validation

时间:2014-10-10 23:51:54      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   sp   div   

It is possible to turn on XML Schema validation during parsing with a SAXParser. Here is how it looks:

Schema schema = null;
try {
  String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
  SchemaFactory factory = SchemaFactory.newInstance(language);
  schema = factory.newSchema(new File(name));
} catch (Exception e) {
    e.printStackStrace();
}


SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setSchema(schema);

SAXParser parser = spf.newSAXParser();

parser.parse(...);

First a Schema object is created from some XML schema file.

Second, a SAXParserFactory is created and this factory has the Schema instance set on it. All SAXParser instances created by this factory will use this XML Schema to validate XML files.

Third, an XML file is parsed and validated.

 

Java SAX Schema Validation

标签:style   blog   color   io   os   ar   java   sp   div   

原文地址:http://www.cnblogs.com/ghgyj/p/4017968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!