Validating an XML file using the RELAX NG schema in Java (IDE-Eclipse)

I am trying to check the xml name of the bookNew.xml file for a .rnc file called bookNewRelax.rnc.

The error that I constantly encounter is

An exception in the "main" thread is java.lang.IllegalArgumentException: No SchemaFactory, which implements the schema language specified: http://relaxng.org/ns/structure/1.0 can be loaded into javax.xml.validation.SchemaFactory.newInstance (Unknown source) on testRelax.main (testRelax.java:38)

To prevent this, I used a line of code before creating an object of the SchemaFactory class, which I thought would help solve this problem. ptece code matters: -

System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); 
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); 

I have included the external jar-jing.jar in my project, and yet the same exception is thrown.

I also imported the com.thaiopensource library. *; and it is highlighted in yellow that it is never used at all. Personally, I think this is a jar file playing spoilsport here, otherwise why would you use the thaiopensource library.

I am inserting a java file under it.

import java.io. *; import java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean;

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. *;

import org.w3c.dom.Document; import org.xml.sax.SAXException;

import com.thaiopensource. *;

public class testRelax {

/** 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--->"+getUserTime());
        //System.out.println("getCpuTime--->"+getCpuTime());
        //System.out.println("startUserTimeNano--->"+startUserTimeNano);
        //System.out.println("startCPUTimeNano--->"+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());
    }
}

}

, java "" RELAX NG Compact ( .rng ), . .

+3
2

Java- RELAX NG SchemaFactory. , , . , Java .

Jing, - . , .

+4

, , jing-20091111.jar .

, jing , . , SchemaFactory , .

, , alexbrn Java . System.setProperty() RELAX NG, JVM.

0

All Articles