Suppose I validate some XML with the following code:
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(localXsdFile);
Validator validator = schema.newValidator();
validator.validate(inputXML);
The constant W3C_XML_SCHEMA_NS_URI has value http://www.w3.org/2001/XMLSchema. Does this validator actually fetch the schema from the network every time it validates? Or is it embedded in the library somewhere? Or is this implementation-dependent and not standardized?
I am wondering because I would prefer to avoid network calls and so would consider saving that schema file locally.
No, it does not fetch any external resources. The string is just an identifier, indicating which schema language you want the factory to support, e.g. XML Schema 1.0 or RELAX NG, and determines the actual concrete SchemaFactory
you get.
You can easily verify this by loading a local XML Schema resource and turn off internet access. It should work.
I would additionally suggest that you use the SchemaFactory#newDefaultInstane()
method, since it gives you secure processing without having to dig up all the needed properties.