Java XML – JDOM2 – SAXBuilder XSD Validating

In the earlier tutorials we saw how to build a JDOM2 document using SAXBuilder. We also saw how to validate the document using DTD while using the SAXBuilder. In this tutorial we look at how to use the SAXBuilder that validates against an XSD. The example below shows how to use an internally defined XSD. The example after that shows how to define an XSD externally.

package com.studytrails.xml.jdom;

import java.io.File;
import java.io.IOException;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders;

public class JdomUsingSAXBuilderXsdValidating {

	private static String file1 = "tomcat-web-xsd-bad.xml";

	private static String file2 = "tomcat-web-xsd.xml";

	public static void main(String[] args) throws JDOMException, IOException {
		// SAXBuilder builder = new SAXBuilder(XMLReaders.XSDVALIDATING);
		// Document jdomDocValidatedFalse = builder.build(new File(file1));
		// throws an error since the XSD validation fails. comment the lines
		// above and rerun the example

		SAXBuilder builder2 = new SAXBuilder(XMLReaders.XSDVALIDATING);
		Document jdomDocValidatedTrue = builder2.build(new File(file2));

		System.out.println(builder2.getSAXHandlerFactory().getClass());
		// prints class org.jdom2.input.sax.DefaultSAXHandlerFactory
		System.out.println(builder2.getJDOMFactory().getClass());
		// class org.jdom2.DefaultJDOMFactory
		System.out.println(builder2.getXMLReaderFactory().getClass());
		// class org.jdom2.input.sax.XMLReaders

	}
}

Download
tomcat-web-xsd-bad.xml
and
tomcat-web-xsd.xml
for the example above. Now lets look at an example that shows how to specify an XSD externally. The example below specifies two methods to specify an XSD externally. Download
commons-dbcp-pom.xml
and
maven-4.0.0.xsd
for the example below.

package com.studytrails.xml.jdom;

import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.jdom2.Document;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaderJDOMFactory;
import org.jdom2.input.sax.XMLReaderSchemaFactory;
import org.jdom2.input.sax.XMLReaderXSDFactory;
import org.xml.sax.SAXException;

public class JdomUsingSAXBuilderExternalXsdValidating {

	private static String file = "commons-dbcp-pom.xml";
	private static String schemaFile = "maven-4.0.0.xsd";

	public static void main(String[] args) throws JDOMException, IOException, SAXException {


         //METHOD 1
         
		// Define a schema factory and a schema
		SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
		Schema schema = schemaFactory.newSchema(new File(schemaFile));
		
		
		// create an XMLReaderJDOMFactory by passing the schema
		XMLReaderJDOMFactory factory = new XMLReaderSchemaFactory(schema);
		// create a SAXBuilder using the XMLReaderJDOMFactory
		SAXBuilder sb = new SAXBuilder(factory);
		Document doc = sb.build(new File(file));
		System.out.println(doc.getRootElement().getName());
		
		//METHOD 2
		
		File xsd = new File(schemaFile);
		//Create the XMLReaderJDOMFacotory directly using the schema file instead of 'Schema'
		XMLReaderJDOMFactory factory2 = new XMLReaderXSDFactory(schemaFile);
		SAXBuilder sb2 = new SAXBuilder(factory2);
		Document doc2 = sb2.build(new File(file));
		System.out.println(doc2.getRootElement().getName());

	}
}


2 thoughts on “Java XML – JDOM2 – SAXBuilder XSD Validating”

Leave a Comment