In this article we shall perform validation of generated XML by
. This demonstrates Woodstox’ validation capabilities while generating XML output.
Create employee.dtd as shown below.
It defines the employee element that contains id, name and salary sub-elements.
Create TestValidationXmlStreamWriter as shown below.
Create a valid XML as per the DTD as shown in ‘validateSuccess’ method (see lines 15-52 below).
When this valid XML is validated against the schema (see line 29 below), no error is thrown.
Create an invalid XML as shown in ‘validateFailure’ method (see lines 54-92 below).
Note that an invalid element ‘title’ is created in the XML (see lines 78-80 below).
When this invalid XML is validated against the schema then an exception is thrown.
The output of the program demonstrating validation success and failure scenarios is shown below:
Download Source Code for this Program