Validating xml with sax parser

by  |  15-Apr-2016 21:59

Alternatively if you use Stax for writing content (or a library that uses or can use stax), Woodstox also directly support validation when using XMLStream Writer.

Here's a blog entry showing how that is done: Hey Stax Man, are there any XMLStream Writers that do pretty-print indenting?

I was surprised that it's not in the standard implementation. I think it's the right way to go, but there seems very little interest in it.

This will start creating DOM objects as it parses the document - wasteful if you aren't going to use them. } Working this way, a SAXException would be thrown at the first error in the xml-file and stops then the validation. How do I recognize an error in the class that invokes the validate-method of my validator? If you are generating XML files programatically, you may want to look at the XMLBeans library.

You should not use the DOMParser to validate a document (unless your goal is to create a document object model anyway). W3C_XML_SCHEMA_NS_URI); // load a WXS schema, represented by a Schema instance Source schema File = new Stream Source(new File("my Schema.xsd")); Schema schema = Schema(schema File); // create a Validator instance, which can be used to validate an instance document Validator validator = Validator(); // validate the DOM tree try { validator.validate(new DOMSource(document)); } catch (SAXException e) { // instance document is invalid! If I use an Error Handler (own class that implements Error Handler) instead, it recognizes all errors, but the try-catch-block of validator.validate does not throw any Exception..

Community Discussion