Friday, 14 August 2015

XML Validation against XSD(s) Utility

There any many tools available to validate an XML document against an XSD. These include operating system scripts and tools such as xmllint, XMLTools plugin in NotePad++ and many online validators. Java makes it easy to write such a tool and this post demonstrates a utility created in Java for XML validation.

Download Utility here (Leave a comment with your e-mail Id in case the utility from the link is not downloading or not working as expected. I will send it to your e-mail)

Created a small Java Utility using Swing which can validate an XML Document against one or more XSDs. The technical details and code is provided here.

The First Look


This is the 1st look of the utility when it will be started. I have tried to make it more user friendly. I have also provided an option to change the theme of your utility.

Changing the Look and Feel of the Utility

 Theme of the utility can be changed using the drop-down menu provided.

Adding an XML File(File Chooser)

 To add an XML document, the browse button is already provided. Click on the button to open the file chooser popup(provided by Swing). Navigate to the file and select a valid XML document. In case you select an invalid XML Doc, the utility will not allow you to proceed.

Adding an XSD File(Add XSD Button)

 To add XSD document(s), we have provided an 'Add XSD File' button using which once can add N number of XSD documents against which the XML needs to be validated. Click on the button to populate the XSD File Browse button as per your convenience. Once Browse button is clicked, file chooser popup(provided by Swing) is displayed. Navigate to the file and select a valid XSD document(s). In case you select an invalid XSD Document, the utility will not allow you to proceed.

Field Validations


Once you have selected both the XML and XSD Documents. Click on Validate XML button. Before validating the XML, the utility will validate whether correct files are being provided or not. An appropriate error message will be displayed(as above) in case of the following.
  1. XML File not selected.
  2. Invalid XML File selected.
  3. XSD File not selected.
  4. Invalid XSD File selected.
Validation Output
Validation Success


 If the XML is perfect as per the XSD(s) provided, the above message will be displayed.

Validation Failure

If the XML is not written as per the XSD(s) provided, the above window will be provided which will contain the list of all the errors in the XML Document in the following format.
ERROR : (Exception message being displayed)
  Line Number: (Line Number of the incorrect element)
  Column Number: (Column Number of the incorrect element line)

No comments:

Post a Comment