首页 > 编程语言 > 详细

Java SAX Schema Validation

时间:2014-10-10 23:33:05      阅读:390      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!