An XML document is considered ‘well-formed’ if it follows the normal rules of XML. In the example below we look at validating an XML document while using DOM, SAX and St AX. On the other hand, an XML is considered valid if it follows the rules specified in the DTD or XSD. To turn on validation, you must request it explicitly.In this tip, I show you how to do that using the SAX API.Because SAX is event driven, you'll want to be notified of, and react to, any errors that occur during validation. XMLReader Factory; public class Validate XML class My Error Handler implements Error Handler as it brings things to a crashing halt when any problems arise.You can do this by using the SAX After making the changes per Listing 1, the parser will perform validation on documents, but you might not hear about any problems it encounters because this code doesn't provide a means to report errors. Instead of gracefully returning an error code to the parent application, I print the error to the screen and bail out of the code.As an example, the element elements are allowed (as in a lengthy novel with hundreds of pages).A DTD or an XML Schema plus a validating parser make a document usable across applications.
Typically, an application validates XML documents against a list of known schemas as part of its error handling.we look at both DTD and XSD validation package com.jaxp; import By now, you have done a lot of experimenting with the nonvalidating parser.The validator checks for well formedness first, meaning that your XML file must be parsable using a DOM/SAX parser, and only then does it validate your XML against the XML Schema.The validator will report fatal errors, non-fatal errors and warnings.