Java Reference
In-Depth Information
Collecting
Schema
Validation
Events
During
Marshaling and Unmarshaling
Problem
You want to learn more about schema validation errors during unmarshaling so you can report
more robust messages back to your user, receive validation failure events as they occur, and
generally gain more control over the validation process.
Solution
Implement the javax.xml.bind.ValidationEventHandler interface to perform custom
operations and set it into your marshaler or unmarshaler object. Also try the Valida-
tionEventCollector utility.
This interface defines one method: handleEvent , which gives you the opportunity to perform
custom event handling. To use it, you implement the interface and then register your imple-
mentation with the marshaler or unmarshaler, which will use your handler to collect validation
events.
NOTE
A validation handler is not the appropriate place for modifying the XML content tree, though it might
be tempting to do so. Treat your handler as a read-only operator that must return false if any fatal
error or runtime exception is thrown.
For your convenience, there are two implementations of ValidationEventHandler available
in the javax.xml.bind package: DefaultValidationEventHandler and Valida-
tionEventCollector . The default handler is used if you do not set one on the un/marshaler
yourself by calling the setEventHandler method. This implementation will continue pro-
cessing if it receives validation warnings, but stop executing immediately upon notice of the
first error.
During validation, the handleEvent method notifies you of validation warnings or errors us-
ing a ValidationEvent object. This object indicates the severity of the event (WARNING,
ERROR, or FATAL_ERROR), messages from the processor, and any exceptions thrown. But
it also gives you access to a ValidationEventLocator object via its getLocator method.
This object gives you access to a rich array of data regarding the file, URL, or DOM node
that caused the event to be emitted. You also get line and column number as well, allowing for
Search WWH ::




Custom Search