开发者

Validating an xml file using RELAX NG Schema in Java (IDE - Eclipse)

I have been trying to validate an xml file name bookNew.xml against an .rnc file named bookNewRelax.rnc.

The error that I constantly face is --

Exception in thread "main" java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://relaxng.org/ns/structure/1.0 could be loaded at javax.xml.validation.SchemaFactory.newInstance(Unknown Source) at testRelax.main(testRelax.java:38)

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:-

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 still, the same exception is being thrown.

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.

I am pasting the java file underneath.

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 n开发者_如何学编程ot valid because--");
        System.out.println(ex.getMessage());
    }
}

}

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.


Java implementations are not required to implement RELAX NG validation via SchemaFactory. So even if it works in one environment, it is not portable. From your error message, it appears your particular Java implementation doesn't support it.

Since you have the Jing libraries, you can validate using them - see the documentation here to get started.


I had the same problem and it turned out that I was missing jing-20091111.jar from the classpath.

I've been using some class loader mechanisms, so all the jing classes were available if I used them in my code. The problem was that SchemaFactory didn't know about my classloaders, so I had to put the jar directly in the classpath.

So I think alexbrn's response about particular Java implementations' support is wrong. When System.setProperty() is used to provide implementation for RELAX NG, it should work in every JVM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜