Woodstox : SAX Parsing Using Woodstox with Validation

Overview

Woodstox supports SAX parsing with (Document Type Definition) DTD validations. Essentially the structure of XML is validated against the DTD specification.

In the following program, we shall explore how to validate an XML document against a DTD by using Woodstox SAX validation capabilitites.

Create employeeSchema.xml file as shown below.

It describes the elements that can valid that can appear in the XML file.

employee element appears as a parent tag (see line 4 below).

Other children tags include id, name and salary of type string(see lines 7-9 below).

Create a valid XML file as shown below. It is in accordance with the schema mentioned above.

Create an XML file as shown below that is invalid.

Note that an invalid element called ‘title is declared (see line 4 below).

Create TestSaxParsingWithValidation as shown below.

Load the XML file (see lines 22-23 below).

Load the schema file (see lines 26-28 below).

Get a handle to the validator and validate XML against the Schema (see lines 29-36 below).

Create the main method (see lines 40-45 below).

Perform validation against valid XML (see line 42 below). This shall result in successful validation.

Then perform validation against invalid XML (see line 44 below). The validation shall fail in this case.

The output of the program demonstrating SAX Validation is shown below:

The validEmployee.xml is successfully validated (see line 1 below).

Validation for invalidEmployee.xml failed (see lines 3-4 below)

Download Source Code for this Program



3 thoughts on “Woodstox : SAX Parsing Using Woodstox with Validation”

Leave a Comment