How to create XML validator from XML schema?
发布时间:2020-12-16 06:11:35 所属栏目:百科 来源:网络整理
导读:How to create XML validator from XML schema? public static void main(String[] args) throws Exception { // http://www.java-tips.org/java-se-tips-100019/170-javax-xml-validation/1733-how-to-create-xml-validator-from-xml-schema.html try { //
How to create XML validator from XML schema? public static void main(String[] args) throws Exception { // http://www.java-tips.org/java-se-tips-100019/170-javax-xml-validation/1733-how-to-create-xml-validator-from-xml-schema.html try { // define the type of schema - we use W3C: String schemaLang = XMLConstants.W3C_XML_SCHEMA_NS_URI; // get validation driver: SchemaFactory factory = SchemaFactory.newInstance(schemaLang); // create schema by reading it from an XSD file: Schema schema = factory.newSchema(new StreamSource( "BuildInstall.xsd")); Validator validator = schema.newValidator(); // at last perform validation: validator.validate(new StreamSource("Install.xml")); } catch (SAXException | IOException ex) { // we are here if the document is not valid: // ... process validation error... ex.printStackTrace(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |