【发布时间】:2011-09-13 09:15:23
【问题描述】:
我一直在尝试针对名为 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 类的对象之前使用了一行代码,我相信这将有助于解决这个问题。代码的ptece如下:-
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.*;它带有黄色下划线,表明它根本没有使用过。个人认为,这里是玩破坏的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 也可以),以便可以完成正确的验证。感谢期待。
【问题讨论】:
-
看起来该代码的一半与您提出的问题无关。请更新您的问题以将其删除。