我一直在尝试针对名为bookNewRelax.rnc的.rnc文件验证xml文件名称bookNew.xml.
我经常面对的错误是-
线程“主”中的异常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),以便可以进行适当的验证.谢谢您的期待.