views:

243

answers:

1
private void validateXML(DOMSource source) throws Exception {
    URL schemaFile = new URL("http://www.csc.liv.ac.uk/~valli/modules.xsd");
    SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
    Schema schema = schemaFactory.newSchema(schemaFile);

    Validator validator = schema.newValidator();
    DOMResult result = new DOMResult();
    try {
        validator.validate(source, result); 
        System.out.println("is valid");
    } catch (SAXException e) {
        System.out.println("not valid because " + e.getLocalizedMessage());
    }
}

But this returns an error saying: Exception in thread "main" java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://www.w3.org/2001/XMLSchema -instance could be loaded

Is this a problem with my code or with the actual xsd file?

+1  A: 

That error means that your installed Java doesn't have any classes that can parse XMLSchema files, so it's not a problem with the schema or your code.

I'm pretty sure recent JREs have the appropriate classes by default, so can you get us the output of java -version?


Update:

You're using the wrong XMLContants string. You want: XMLConstants.W3C_XML_SCHEMA_NS_URI

Brabster
java version "1.6.0_17"Java(TM) SE Runtime Environment (build 1.6.0_17-b04)Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)
Becky