DOM Parser: Schema Validator in Java

Connect with

DOM parser schema validation in javaExample of DOM parser schema validation in Java. Validate your XML schema using DOM parser and it is easy to validate XML data whether format is correct or not.

1. Overview of DOM parser schema validation

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). You can visit different XML parsers comparison, pull parser vs push parser.

2. 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

 
strong

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

3. Write XSD for XML

The following XSD schema class is corresponding to XML structure.



	
	
          
		
		
		
         
	
	
	
	 	
	 		
	 	
	
	

4. Write Validator class

package com.mysoftkey.dom.validator;

import java.io.File;

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
   System.out.println(e.toString());
  }
 }

 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 java.io.File, but
    * you could use a java.net.URL or a javax.xml.transform.Source instead.
    */
   schema = factory.newSchema(new File(schemaFileName));
  } catch (Exception e) {
   System.out.println(e.toString());
  }
  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) {
   System.out.println(e.toString());
  }
  return document;
 }

}

Output with valid xml and XSD:

Validator Class: com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl
Validation passed.

Change in XML as follows:


	This is heading
	My first HTML document in XML format.











My div container

In the added div element tag so it’s not valid as per XSD.

Output with Invalid xml or XSD:
This is the output of DOM parser Schema validation in Java.

Validator Class: com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl
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.

Visit Oracle Java Official Site for more details.
Your comments are welcome to improve this post validating XML Schema using DOM parser. Happy Learning 🙂


Connect with

Leave a Comment

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