SAX Parser : Schema Validator in Java

Connect with

Oracle Java
This post is about validation of XML content or node, using XSD (XML Schema Definition) language in java language. if you have xml you can write logic to in xsd class in which you can write your logic: what are the node name, what type of data, how much occurrence of node , with what type of value etc.

1. Sample XML and Steps

File: myCustomer.xml Sample custom xml, which you have to validate.



  Tanisha Jha
  5

Steps of process.
– First of all write xsd (XML schema Definition ) for the provided XML.
– Write you custom java code to valid the XML

2. XSD (myCustomer.xsd) for the provided XML

File: myCustomer.xsd XSD for the provided XML.




  

    
	
	   
	   
	
   


3. Java Class to validate XML using XSD

file: SAXSchemaValidatorExample.java
this is entry point of this demo to run with mail method.

package com.mysoftkey.sax.validator;

import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.transform.sax.SAXSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.InputSource;

/**
 * This class is used to demonstrate validation of XML with provided XSD (Schema)
 * 
 * @author ranjeet.jha
 *
 */
/**
 * @author anushka
 *
 */
public class SAXSchemaValidatorExample {

 public static void main(String[] a) {

  String schemaName = "./src/com/mysoftkey/sax/validator/myCustomer.xsd";
  String xmlName = "./src/com/mysoftkey/sax/validator/myCustomer.xml";
  Schema schema = loadSchema(schemaName);

  // call validate method to validate the xml with schema
  validateXml(schema, xmlName);
 }

 public static void validateXml(Schema schema, String xmlName) {
  try {
   // creating a Validator instance
   Validator validator = schema.newValidator();
   System.out.println();
   System.out.println("Validator Class: " + validator.getClass().getName());

   // preparing the XML file as a SAX source
   SAXSource source = new SAXSource(new InputSource(new java.io.FileInputStream(xmlName)));

   // validating the SAX source against the schema
   validator.validate(source);
   System.out.println("Validation passed.");

  } catch (Exception e) {
   // catching all validation exceptions
   System.out.println(e.toString());
  }
 }

 /**
  * This method is used to load the schema name provide to the method.
  * 
  * @param name
  * @return
  */
 public static Schema loadSchema(String name) {
  Schema schema = null;
  try {
   String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
   SchemaFactory factory = SchemaFactory.newInstance(language);
   schema = factory.newSchema(new File(name));
  } catch (Exception e) {
   System.out.println(e.toString());
  }
  return schema;
 }

}

Output: with valid content

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

5. With Invalid XML or XSD

Output with invalid content. for this add any one element anywhere in the myCustomer.xml file and run the application you can find following output.

Validator Class: com.sun.org.apache.xerces.internal.jaxp.validation.ValidatorImpl
org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 11; cvc-complex-type.2.4.d: Invalid content was found starting with element 'address'. No child element is expected at this point.

changed for invalid xml as:



   Tanisha Jha
   5 
   
New Delhi, India

element address added after age element, so as per myCustomer.xsd this is invalid.

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


Connect with

Leave a Reply

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