使用Java中的RELAX NG Schemavalidationxml文件(IDE – Eclipse)

我一直在尝试针对名为bookNewRelax.rnc的.rnc文件validationxml文件名bookNew.xml。

我经常遇到的错误是 –

线程“main”中的exceptionjava.lang.IllegalArgumentException:没有实现以下指定的模式语言的SchemaFactory: http : //relaxng.org/ns/structure/1.0可以在javax.xml.validation.SchemaFactory.newInstance(未知)加载来自testRelax.main(testRelax.java:38)

为了防止这种情况,我在实例化SchemaFactory类的对象之前使用了一行代码,我相信这有助于解决这个问题。 代码的含义如下: –

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

我已经在我的项目中包含了外部jar-jing.jar,但仍然会抛出同样的exception。

我还导入了com.thaiopensource库。*; 它以黄色下划线表示它从未被使用过。 我个人认为,这是播放spoilsport的jar文件,否则为什么thaiopensource库永远不会被使用。

我正在粘贴下面的java文件。

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

公共类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 Schema(或者简单地.rng也会这样做),以便可以进行适当的validation。 谢谢你的期待。

通过SchemaFactory实现RELAX NGvalidation不需要Java实现。 因此,即使它在一个环境中工作,也不可移植。 从您的错误消息,您的特定Java实现似乎不支持它。

由于您有Jing库,您可以使用它们进行validation – 请参阅此处的文档以开始使用。

我有同样的问题,结果发现我从类路径中缺少jing-20091111.jar。

我一直在使用一些类加载器机制,所以如果我在我的代码中使用它们,所有jing类都可用。 问题是SchemaFactory不知道我的类加载器,所以我不得不将jar直接放在类路径中。

所以我认为alexbrn对特定Java实现支持的回应是错误的。 当System.setProperty()用于为RELAX NG提供实现时,它应该适用于每个JVM。