问题描述
||
我一直在尝试针对名为bookNewRelax.rnc的.rnc文件验证xml文件名称bookNew.xml。
我经常面对的错误是-
线程\“ main \” java.lang.IllegalArgumentException中的异常:无法加载实现由http://relaxng.org/ns/structure/1.0指定的模式语言的SchemaFactory。
在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,但仍然引发了相同的异常。
我还导入了库com.thaiopensource。*;并用黄色下划线表示根本没有使用过。我个人认为,这是在这里运行spoilsport的jar文件,否则为什么Thaiopensource库永远不会使用。
我将Java文件粘贴在下面。
导入java.io. *;
导入java.lang.management.ManagementFactory;
导入java.lang.management.ThreadMXBean;
导入javax.xml.XMLConstants;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.ParserConfigurationException;
导入javax.xml.transform.dom.DOMSource;
导入javax.xml.validation。*;
导入org.w3c.dom.Document;
导入org.xml.sax.SAXException;
进口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),以便可以进行正确的验证。谢谢您的期待。
解决方法
通过SchemaFactory进行RELAX NG验证不需要Java实现。因此,即使它在一种环境中工作,它也不是便携式的。从错误消息中,您的特定Java实现似乎不支持它。
由于您具有Jing库,因此可以验证是否使用它们-请参阅此处的文档以开始使用。
, 我有同样的问题,结果是我从类路径中缺少jing-20091111.jar。
我一直在使用某些类加载器机制,因此如果在代码中使用它们,则所有jing类都可用。问题是SchemaFactory不了解我的类加载器,因此我不得不将jar直接放在类路径中。
因此,我认为alexbrn对特定Java实现的支持是错误的。当System.setProperty()用于提供RELAX NG的实现时,它应该在每个JVM中都能工作。