SAX Parser : Schema Validator in Java

Connect with

Java SAX parser schema validationJava SAX parser schema validation is about validation of XML content or node using XSD. Java SAX schema validation is important when you parsing XMl to Java.

1. Overview of Java SAX parser schema validation

This post is about the validation of XML content or node, using XSD (XML Schema Definition) language in java language. If you have XML you can write the logic in an XSD file, logic can be: what are the node name, what type of data, how many occurrences of the node, with what type of value, etc.

2. Sample XML and Steps for Schema validation Java

In this step, you can find XML in which you have Java SAX parser schema validation.
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

3. XSD for Java SAX Schema Validation

Find the example of XSD for Java SAX parser schema validation example.
File: myCustomer.xsd XSD for the given XML.




  

    
	
	   
	   
	
   


4. Java Class to validate XML using XSD

file: SAXSchemaValidatorExample.java
This is entry point using main method for Java SAX parser schema validation example .

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 for Java SAX schema validation of XML with provided XSD (Schema)
 * 
 * @author ranjeet.jha
 *
 */
public class JavaSAXSchemaValidationExample {

 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.

6. Related Posts on Parser

In order to understand the difference between DOM and SAX, you have to understand each one of the parsers. For this you can visit.

7. Reference

You can visit Oracle Java for more details.

Thanks for visiting this post for the Java SAX parser schema validation example. You can also visit Core Java Tutorial Listing page for more articles on Core Java examples.
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 *