Java Reference
In-Depth Information
This validation is one benefit XML has over JSON. You can supply your own validation lo-
gic when parsing JSON documents, but with XML, the parser can perform the validation for
you. That benefit is not without cost in terms of performance.
XML validation is done against one more more schema or DTD files. Although validating
against DTDs is faster, XML schemas are more flexible, and their use now predominates in
the XML world. One thing that makes schemas slower than DTDs is that schemas are usu-
ally specified in different files. So the first thing that can be done to reduce the penalty for
validation is to consolidate the schema files: the more schema files that need to be processed,
the more expensive validation is. There is a trade-off here between the maintainability of the
separate files and the performance gain involved. Unfortunately, schema files are not easy to
combine (e.g., as CSS or JavaScript files are), since they maintain different namespaces.
The location from which schema files are loaded can be a significant source of performance
issues. If a schema or DTD must be repeatedly loaded from the network, performance will
suffer. Ideally, the schema files should be delivered along with the application code so that
the schema(s) can be loaded from the local filesystem.
For normal validation with SAX, the code simply sets some properties on the SAX parser
factory (this procedure applies only to SAX parsers; StAX parsers can validate against DTD
files, but not schemas, unless they use a Validator object as discussed later in this section):
SAXParserFactory spf = SAXParserFactory . newInstance ();
spf . setValidating ( true
true );
spf . setNamepsaceAware ( true
true );
SAXParser parser = spf . newSAXParser ();
// Note: above lines can be executed once to create a parser. If
// reusing a parser, instead call parser.reset() and then set its
// properties
parser . setProperty ( JAXPConstants . JAXP_SCHEMA_LANGUAGE ,
XMLReader xr = parser . getXMLReader ();
xr . setErrorHandler ( new
new MyCustomErrorHandler ());
The default for the parser is for it to be nonvalidating, so the first call needed is to the
setValidating() method. Then a property must be set to tell the parser which language to
validate against—in this case, the W3C XML schema language (e.g., XSD files). Finally, all
validating parsers must set an error handler.
Search WWH ::

Custom Search