Creating an XML Schema from a URL works, but does it fail from a local file?

I need to check XML Schema Instance (XSD) documents that are programmatically generated, so I use the following Java snippet that works fine:

SchemaFactory factory = SchemaFactory.newInstance(
    XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema xsdSchema = factory.newSchema( // Reads URL every time...
    new URL("http://www.w3.org/2001/XMLSchema.xsd"));
Validator xsdValidator = xsdSchema.newValidator();
xsdValidator.validate(new StreamSource(schemaInstanceStream));

However, when I save the XML schema file locally and reference it as follows:

Schema schema = factory.newSchema(
    new File("test/xsd/XMLSchema.xsd"));

The following exception failed:

org.xml.sax.SAXParseException: schema_reference.4: Failed to read the schema document file: /Users/foo/bar/test/xsd/XMLSchema.xsd because 1) could not find the document; 2) the document cannot be read; 3) the root element of the document is not <xsd: schema>.

, , exists() canRead() File. (-, wget), , .

, XSD, URL- HTTP, ?

[]

, factory.newSchema(...), Readers InputStreams ( ) - ​​ . , , . .

, , XML Schema, , XMLSchema.xsd , schemaLocation . , :

@Blaise Doughan @Tomasz Nurkiewicz .

+5
2

UPDATE

XMLSchema.xsd , ?


. , , .

org.xml.sax.SAXParseException: schema_reference.4: :/Users/foo/bar/test/xsd/XMLSchema.xsd ', 1) ; 2) ; 3) .

+2

All Articles