title: Validating an xml file using RELAX NG Schema in Java (IDE - Eclipse)
body: <p>I have been trying to validate an xml file name bookNew.xml against an .rnc file named bookNewRelax.rnc.</p>
  
  <p>The error that I constantly face is -- </p>
  
  <p>Exception in thread "main" java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: <a href="http://relaxng.org/ns/structure/1.0" rel="nofollow">http://relaxng.org/ns/structure/1.0</a> could be loaded
      at javax.xml.validation.SchemaFactory.newInstance(Unknown Source)
      at testRelax.main(testRelax.java:38)</p>
  
  <p>In order to prevent this, I used a line of code before instantiating an object of the SchemaFactory class, which I believed would help solve this issue. the ptece of code is as under:-</p>
  
  <pre><code>System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); 
  SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); 
  </code></pre>
  
  <p>I have included the external jar - jing.jar in my project and still, the same exception is being thrown.</p>
  
  <p>I have also imported the library com.thaiopensource.*; and it is underlined in yellow showing that it is never used at all. Personally, I think, it is the jar file playing spoilsport here, else why would the thaiopensource library be never come into use.</p>
  
  <p>I am pasting the java file underneath. </p>
  
  <p>import java.io.*;
  import java.lang.management.ManagementFactory;
  import java.lang.management.ThreadMXBean;</p>
  
  <p>import javax.xml.XMLConstants;
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  import javax.xml.parsers.ParserConfigurationException;
  import javax.xml.transform.dom.DOMSource;
  import javax.xml.validation.*;</p>
  
  <p>import org.w3c.dom.Document;
  import org.xml.sax.SAXException;</p>
  
  <p>import com.thaiopensource.*;</p>
  
  <p>public class testRelax {</p>
  
  <pre><code>/** Get CPU time in nanoseconds. */
  public static long getCpuTime( ) {
      ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
      return bean.isCurrentThreadCpuTimeSupported( ) ?
          bean.getCurrentThreadCpuTime( ) : 0L;
  }
  
  /** Get user time in nanoseconds. */
  public static long getUserTime( ) {
      ThreadMXBean bean = ManagementFactory.getThreadMXBean( );
      return bean.isCurrentThreadCpuTimeSupported( ) ?
          bean.getCurrentThreadUserTime( ) : 0L;
  }
  
  
  
  public static void main(String args[]) throws SAXException, IOException, ParserConfigurationException {
  
      System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); 
      SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); 
  
      File schemaLocation = new File("C:/Users/gp85943/workspace/TestBookRelax/src/bookNewRelax.rnc");
      Schema schema = factory.newSchema(schemaLocation);
      Validator validator = schema.newValidator();
  
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
      domFactory.setNamespaceAware(true);
      DocumentBuilder builder = domFactory.newDocumentBuilder();
      File file=new File("C:/Users/gp85943/workspace/TestBookRelax/src/bookNew.xml");
  
  
      try{
  
          long startTime = System.currentTimeMillis();
          System.out.println("Milli"+startTime);
          long startUserTimeNano   = getUserTime( );
          System.out.println("Nano"+startUserTimeNano);
          long startCPUTimeNano   = getCpuTime( );
          System.out.println("Nano"+startCPUTimeNano);
  
          Document doc = builder.parse(new File("C:/Users/gp85943/workspace/TestBookRelax/src/bookNew.xml"));
          DOMSource source = new DOMSource(doc);
  
          validator.validate(source);
  
          long stopTime = System.currentTimeMillis();
          System.out.println("MilliStop"+stopTime);
          long elapsedTime = stopTime - startTime;
          System.out.println("Elapsed time"+elapsedTime);
          //System.out.println("getUserTime---&gt;"+getUserTime());
          //System.out.println("getCpuTime---&gt;"+getCpuTime());
          //System.out.println("startUserTimeNano---&gt;"+startUserTimeNano);
          //System.out.println("startCPUTimeNano---&gt;"+startCPUTimeNano);
          long taskUserTimeNano    = getUserTime( ) - startUserTimeNano;
          System.out.println("User"+taskUserTimeNano);
          long taskCpuTimeNano    = getCpuTime( ) - startCPUTimeNano;
          System.out.println("CPU"+taskCpuTimeNano);
          System.out.println(file + " The document is valid");
  
  
      }
  
      catch(SAXException ex)
      {
          System.out.println("the document is not valid because--");
          System.out.println(ex.getMessage());
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Kindly advise me how to make my java program "accept" the RELAX NG Compact Schema (or else simply .rng will also do) so that the proper validation may be done. Thanks in anticipation. </p>