new SAXSource(InputSource inputSource)
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class MainClass {
public static void main(String[] args) throws IOException {
File documentFile = new File(args[0]);
File schemaFile = new File(args[1]);
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = null;
try {
schema = factory.newSchema(schemaFile);
} catch (SAXException e) {
fail(e);
}
Validator validator = schema.newValidator();
SAXSource source = new SAXSource(new InputSource(new FileReader(documentFile)));
try {
validator.validate(source);
} catch (SAXException e) {
fail(e);
}
}
static void fail(SAXException e) {
if (e instanceof SAXParseException) {
SAXParseException spe = (SAXParseException) e;
System.err.printf("%s:%d:%d: %s%n", spe.getSystemId(), spe.getLineNumber(), spe
.getColumnNumber(), spe.getMessage());
} else {
System.err.println(e.getMessage());
}
System.exit(1);
}
}
Related examples in the same category