最近对Schema 验证研究了一下, 代码如下: 所需JAR包需要自己下载(版本一定要正确)
public class SchemaValidation {
public static void main(String[] args) { validate(); }
public static void validate() { try { SchemaValidation demo = new SchemaValidation(); // Give the xml and schema name InputStream xmlString = demo.getClass().getResourceAsStream("GBAInit.xml"); InputStream schemaStr = demo.getClass().getResourceAsStream("GBAInitSchema.xsd"); SAXReader reader = createSAXReader(schemaStr); System.out.println("XSD parse successfully !"); Document document = reader.read(xmlString); System.out.println("Successfully validation .. . "); } catch (DocumentException e) { System.out.println("Exception occurred: " + e); Throwable nestedException = e.getNestedException(); if (nestedException != null) { System.out.println("NestedException: " + nestedException); nestedException.printStackTrace(); } else { e.printStackTrace(); } } catch (Throwable t) { System.out.println("Exception occurred: " + t); t.printStackTrace(); } }
/** Registers the Verifier with the SAXReader */ protected SAXReader createSAXReader(InputStream schemaURI) throws Ex[1] [2] 下一页
|
|