Validation - Java working with XML overview (tutorial)

Complete Source Code for all examples below

Validation of XML is done against the schema. The schema is the specification for how our XML may look like. Java merely provides a mechanism of validation the schema rules against a given XML.

The schema is very relaxed in the way it can be organised. You may just do all the configurations inline, or extract some of them using ref (references), or event make your own hierarchy of complex types (named types) to make your schema tidy.

Here I will give you three versions of the schema that produce exactly the same result to give you a hint to how you can do it.

 

The XML:

 




  John Smith
  2011-02-14
  
    Sherlock Holmes
    
221b, Baker Str
London NW1 6XE UK
Smoking pipe Special Edition 1 10.90 Violin 1 99.99

The basic schema:

 



	
	  
	    
	    	
	    	
	    	
	    	
			  
			    
			      
			      
			      
			      
			    
			  
			
			
			  
			    
			      
			      
			      
			      
			    
			  
			
	    
	    
	    
	  
	


The schema with ref's:

 





















  
    
      
      
      
      
    
  



  
    
      
      
      
      
    
  



  
    
      
      
      
      
    
    
  




The schema with named types:

 





  



  



  



  



  
    
  



  
    
    
    
    
  



  
    
    
    
    
  



  
    
    
    
    
  
  






The java code to validate:

 

package dp.test.xml.xsd;

import java.io.IOException;
import java.io.StringWriter;

import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.junit.Test;
import org.xml.sax.SAXException;


public class XSDValidateExample
{
	
	@Test
	public void testSimpleSchema() throws SAXException {
	
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		Schema schema = factory.newSchema(XSDValidateExample.class.getResource("order.xsd"));
		Validator validator = schema.newValidator();
				
		StringWriter resWriter = new StringWriter();
		Result results = new StreamResult(resWriter);
		try {
			validator.validate(new StreamSource(XSDValidateExample.class.getResourceAsStream("order.xml")), results);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	@Test
	public void testSchemaWithReferences() throws SAXException {
		
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		Schema schemaRef = factory.newSchema(XSDValidateExample.class.getResource("order-using-refs.xsd"));
		Validator validatorRef = schemaRef.newValidator();
		
		StringWriter resRefWriter = new StringWriter();
		Result resultsRef = new StreamResult(resRefWriter);
		try {
			validatorRef.validate(new StreamSource(XSDValidateExample.class.getResourceAsStream("order.xml")), resultsRef);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
	}
	
	@Test
	public void testSchemaWithNamedTypes() throws SAXException {
		
		SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		Schema schemaNamed = factory.newSchema(XSDValidateExample.class.getResource("order-using-named-types.xsd"));
		Validator validatorNamed = schemaNamed.newValidator();
		
		StringWriter resNamedWriter = new StringWriter();
		Result resultsNamed = new StreamResult(resNamedWriter);
		try {
			validatorNamed.validate(new StreamSource(XSDValidateExample.class.getResourceAsStream("order.xml")), resultsNamed);
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}
		
	}
	
}


 

This page was last updated on: 13/04/2012 11:10