BValid is a Java API designed for fast, easy-to-use validation of XML documents. It defines and implements a couple high-level Java interfaces to make the validation process simple, yet flexible.
Distinguishing features:
Read the release notes to determine what has changed for this release.
The latest distribution (source and binary) can be downloaded from http://www.sf.net/projects/bvalid
After unzipping the binary distribution, you should be able to use the
bvalid
command-line utility right away.
To begin using bvalid in your own projects, you'll need the required jars (included in the lib/ directory) in your CLASSPATH.
Note: The binary distribution is compiled for Java 1.4. Currently, BValid will compile and run with Java 1.4 and Java 1.5.
The main interface you work with is the Validator. Once you have obtained an instance from the ValidatorFactory, you can use it to validate any number of XML documents from any number of concurrent threads.
To become familar with the API, see the examples below, then check out the API Javadocs for more detailed information.
The following example uses a validator without any special configuration.
Validator v = ValidatorFactory.getValidator(SchemaLanguage.XSD, null); try { File doc = new File("mydoc.xml"); v.validate(new FileInputStream(doc)); System.out.println("Validation SUCCESSFUL!"); } catch (ValidationException e) { System.out.println("Validation FAILED: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } }
This next example uses a validator that automatically caches schema files to disk, and keeps parsed grammars in memory for re-use.
File cacheDir = new File("mySchemaCache"); cacheDir.mkdirs(); Map opts = new HashMap(); opts.put(ValidatorOption.CACHE_PARSED_GRAMMARS, "true"); Validator v = ValidatorFactory.getValidator(SchemaLanguage.XSD, cacheDir, opts); try { File doc = new File("mydoc.xml"); v.validate(new FileInputStream(doc)); System.out.println("Validation SUCCESSFUL!"); } catch (ValidationException e) { System.out.println("Validation FAILED: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } }
The final example, below, is more advanced. It demonstrates using a SchemaLocator backed by a pre-populated, memory-based SchemaCatalog. It also instructs the validator NOT to fail if the instance document references a schema that isn't in the catalog. This effectively causes the validator to ONLY use our local schema copies (when referenced), and to skip validation for parts of the document that point to other schemas.
File schema1 = new File("my-schema1.xsd"); File schema2 = new File("my-schema2.xsd"); SchemaCatalog catalog = new MemorySchemaCatalog(); catalog.put("http://example.org/schema1.xsd", new FileInputStream(schema1)); catalog.put("http://example.org/schema2.xsd", new FileInputStream(schema2)); SchemaLocator locator = new CatalogSchemaLocator(catalog); Map opts = new HashMap(); opts.put(ValidatorOption.CACHE_PARSED_GRAMMARS, "true"); opts.put(ValidatorOption.FAIL_ON_MISSING_REFERENCED, "false"); Validator v = ValidatorFactory.getValidator(SchemaLanguage.XSD, locator, opts); try { File doc = new File("mydoc.xml"); v.validate(new FileInputStream(doc)); System.out.println("Validation SUCCESSFUL!"); } catch (ValidationException e) { System.out.println("Validation FAILED: " + e.getMessage()); if (e.getCause() != null) { e.getCause().printStackTrace(); } }
The bvalid
command-line utility is a simple application of the API that
can be used to validate a single XML document at a time.
To use it,
change to the directory where the BValid binary distribution has been installed,
and type bvalid
. If you want to run it from any
directory, set the BVALID_HOME
environment variable to the installation
directory, then add it to your PATH
.
Note: In Unix, you will need to make the bvalid script executable before running
it. This can be done with chmod 755 bvalid
Running bvalid -h
displays the following usage information.
Usage: bvalid [OPTIONS] LANG XMLFILE Or: bvalid --version Or: bvalid --help Where: LANG a supported schema language, such as xsd XMLFILE the path to the instance file to validate Options: -cf, --cache-files Cache schema files locally -co, --cache-objects Cache parsed grammar objects in memory -am, --allow-missing Allow missing referenced schemas. If the instance includes references to schemas that can't be found, this will skip them rather than failing validation. --repeat=n Repeat the validation n times (for testing) --schema=file Use the given schema file (url or filename) -v, --version Print version and exit (exclusive option) -h, --help Print help and exit (exclusive option)
ValidationException
currently only reports multiple errors as a multi-line string. An alternate method (getErrorList?) would be nice.BValid is distributed under the Educational Community License (ECL), v1.0.
The distribution also includes several third-party, open-source libraries, each with it's own license terms.
See the License Information Page for specific terms of all relevant licenses.