Validating XML using XSD

Lately, I have been working on a use case where I was asked to parse XML for its validation and retrieve its values.

There are two different document type definitions that can be used with XML:

  • DTD – The original Document Type Definition
  • XML Schema – An XML-based alternative to DTD

A document type definition defines the rules and the legal elements and attributes for an XML document. An XML document validated against a DTD or XSD is both “Well Formed” and “Valid”.

In this blog, I am going to discuss the latter approach. So, let’s just begin with it.

XML Schema

As the name suggests, XML Schema describes the structure of XML just like DTD. To check if your XML file is a “valid” and “well-formed” XML then we need to validate it against its schema file i.e, its XSD.
XSD is XML Schema Definition. Without going into mere theory, let’s dive into its usage.
Consider a sample books.xml file:
<?xml version="1.0"?>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <description>An in-depth look at creating applications
      with XML.</description>

XSD for this XML would look something like this:

<?xml version="1.0"?>
<xs:schema xmlns:xs="" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="catalog" type="catalogType"/>
  <xs:complexType name="bookType">
      <xs:element type="xs:string" name="author"/>
      <xs:element type="xs:string" name="title"/>
      <xs:element type="xs:string" name="genre"/>
      <xs:element type="xs:float" name="price"/>
      <xs:element type="xs:date" name="publish_date"/>
      <xs:element type="xs:string" name="description"/>
    <xs:attribute type="xs:string" name="id"/>
  <xs:complexType name="catalogType">
      <xs:element type="bookType" name="book"/>


Now, let’s do what we intended to do for this blog: Validate this XML file with its XSD.

For that we need an object of SchemaFactory class using the default XML schema provided by W3C , using this object we create an instance of Schema class for our XSD file:

val factory: SchemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)

val schema: Schema = factory.newSchema(new File(xsdPath))

The schema factory constant here is the string which defines XSDs.

After that create a new Validator object.

val validator: Validator = schema.newValidator()

Now let’s validate this using the validate() method of Validator class.

validator.validate(new StreamSource(new File(xmlPath)))

This method takes the StreamSource of our XML file. The return type of validate() is unit. But this method throws an exception when we try to match our XML file with an incompatible XSD file. Hence let’s wrap this snippet in the try-catch block.

    try {
      val validator: Validator = schema.newValidator()
      validator.validate(new StreamSource(new File(xmlPath)))
    } catch {
      case NonFatal(ex) =>
        ex.printStackTrace() //use logging here

The above code reads XML and XSD paths and then validates the XML file with XSD file.

So, that was one of the easiest ways to validate XML file. You can find the complete implementation here.

I hope it was helpful. Happy Coding! 🙂


This entry was posted in Scala. Bookmark the permalink.

One Response to Validating XML using XSD

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s