Java tutorial
//package com.java2s; //License from project: Apache License import java.io.FileNotFoundException; import java.io.InputStream; import javax.xml.transform.stream.StreamSource; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import org.xml.sax.SAXException; public class Main { public static Schema getSchemaFromResource(String... files) throws SAXException, FileNotFoundException { InputStream[] inputStreams = new InputStream[files.length]; Schema schema = null; try { for (int i = 0; i < files.length; i++) { inputStreams[i] = loadResourceInputStream(files[i]); } schema = getSchema(inputStreams); } finally { for (InputStream is : inputStreams) { try { if (is != null) { is.close(); } } catch (Exception ex) { } } } return schema; } private static InputStream loadResourceInputStream(String file) throws FileNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream inputStream = classLoader.getResourceAsStream(file); if (inputStream == null) { throw new FileNotFoundException("File: '" + file + "', not found."); } return inputStream; } public static Schema getSchema(InputStream... inputs) throws SAXException { StreamSource[] streamSource = new StreamSource[inputs.length]; for (int i = 0; i < inputs.length; i++) { streamSource[i] = new StreamSource(inputs[i]); } SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = sf.newSchema(streamSource); return schema; } }