DOM Parser: Schema Validator in Java

Connect with

Oracle Java
You have an XML file which you have to validate whether the format or data of incoming XML is valid or not. what will you do to achieve this, whether you read and then check one by one or at one go validate all node/elements?

In this, I’m trying to demonstrate validation of XML in DOM parser using XSD (Xml Schema Definition).

1. Approach of Validation of XML data

Let us consider and example of XML content as:

	This is heading
	My first HTML document in XML format.

If anything other than this means not valid XML. Means, only


 element in side the XML, other than this is considered as invalid.

2. Write XSD for XML

The following XSD class is corresponding to XML structure.


3. Write Validator class

package com.mysoftkey.dom.validator;


import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;

 * @author Ranjeet.Jha
public class XSDSchemaDomValidatorExample {

 public static void main(String[] args) {
  // actual location where .xsd and .xml reside
  String schemaName = "./src/com/mysoftkey/dom/validator/myHtml.xsd";
  String xmlName = "./src/com/mysoftkey/dom/validator/myHtml.xml";

  Schema schema = loadSchema(schemaName);
  Document document = parseXmlDom(xmlName);

  validateXml(schema, document);

 public static void validateXml(Schema schema, Document document) {
  try {
   // 3. Get a validator from the schema.
   Validator validator = schema.newValidator();
   System.out.println("Validator Class: " + validator.getClass().getName());

   // validating the document against the schema
   validator.validate(new DOMSource(document));
   System.out.println("Validation passed.");

  } catch (Exception e) {
   // catching all validation exceptions

 public static Schema loadSchema(String schemaFileName) {
  Schema schema = null;
  try {
   //// 1. Lookup a factory for the W3C XML Schema language
   String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
   SchemaFactory factory = SchemaFactory.newInstance(language);

    * 2. Compile the schema. Here the schema is loaded from a, but
    * you could use a or a javax.xml.transform.Source instead.
   schema = factory.newSchema(new File(schemaFileName));
  } catch (Exception e) {
  return schema;

 public static Document parseXmlDom(String xmlName) {
  Document document = null;
  try {
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   document = builder.parse(new File(xmlName));
  } catch (Exception e) {
  return document;


Output with valid xml and XSD:

Validator Class:
Validation passed.

Change in XML as follows:

	This is heading
	My first HTML document in XML format.

My div container

At the line, no 6 added div element tag so it’s not valid as per XSD.

Output with Invalid xml or XSD:

Validator Class:
org.xml.sax.SAXParseException; cvc-complex-type.2.4.d: Invalid content was found starting with element 'div'. 
No child element is expected at this point.

Your comments are welcome to improve this post. Happy Learning 🙂

Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *