XML Validation – Why Validate XML?

validationxml

I am looking for an explanation on just why XML needs to be validated. I have been testing DTD's and Schema's for the past month and recently tried to find out why I am doing this. I know it makes the XML better and more Semantic but what are the overall benefits of validating XML?

Best Answer

To make sure that it works. Sure, if you only write out some data for your own application to read it may be enough if it just works. If you send a file to somebody else matters may be different. But even within your application you may later choose to switch the parsing library and the new one may complain about errors the old one accepted and ignored like not properly escaping some characters.

Or, depending on the kind of data you store, some new characters may appear (maybe a user with a foreign name) and from that moment proper character set definitions may be important. Same if you just send an XML file from Windows to a Linux system. The closer you keep to definitions and validations the less likely you run in unexpected trouble when things change.