How to validate XML vs XSD in Java

In this tutorial, we will use XML Validation API to validate XML vs XSD in Java. Specifically, we will use the javax.xml.validation.Validator class to validate XML vs XSD in this program.

Now, we see the sample XSD and XML files, below.

And you should create xml file from xsd file by using eclipse. See the figure below.

validate xml against xsd
validate xml against xsd

You will be get the xml such as:

This tutorial shows true validation case and false validation case, so you should create other xml file such as:

The program that is implemented to validate two files XML against the XSD. The main point of this program is the validateXML method that returns true if validation is successful or else returns false.

Here is the output:

Hope that this tutorial is interesting with you. All suggestions are welcome.
Happy learning!
You can download source of the program

One thought on “How to validate XML vs XSD in Java

Leave a Comment


Please share it if you found this useful
Hide Buttons