
On 4/23/06, Peter Dimov <pdimov@mmltd.net> wrote:
Daniel Walker wrote:
On 4/23/06, Thorsten Ottosen <thorsten.ottosen@dezide.com> wrote:
Daniel Walker wrote:
Why is what better? Maybe I wasn't clear. When an XML file includes a schema and fails validation when loaded, you do get a run-time exception. I was trying to say that's a good thing. An XML validating parser is similar to a compiler for a strongly typed language: it catches type errors (in addition to syntax errors) immediately before you actually try to use the file.
Ok, why is that exception better than the one I generate if I don't meet the tag I expect, of if the contents of a tag is not of the type I expect?
Well, you don't have to write C++ code to check if the tag/content is what you expect. You declare the acceptable content (tags, attribute/values, branch structures/sub-trees, etc) for your config file in a schema, and the parser determines whether or not the XML file conforms to the format declared in the schema. It has time-saving, organization, and code-reuse advantages among others. Most importantly, it has correctness advantages because one bug you don't have to worry about when you're traversing the tree is running into a tag you don't expect. By the time the file is loaded, all the tags, tree structure, etc. has been validated by the parser and is guaranteed to be the format you expect.
I wouldn't omit the checks in the code, even when using a validating parser. When the schema and the program logic disagree, the program logic "wins". Or crashes. Either way, the schema loses. :-)
Good point about crashes. It is still possible to write programs that crash even when using a schema. There is no silver bullet. Whether or not you want to double check the parser/scheme is the same sort of decision as whether or not you define NDEBUG in a released/deployed system. Sure, the asserts are still useful, but do you really need the extra check? The answer probably depends on the specific circumstances and is somewhat a matter of taste. I think the more common case is that you forget to manually check a constraint on the XML in your code, in which case you would be glad if you had a validating parser. Daniel Walker