使用Java(IDE-Eclipse)中的RELAX NG Schema验证xml文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了使用Java(IDE-Eclipse)中的RELAX NG Schema验证xml文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4055字,纯文字阅读大概需要6分钟。
内容图文
![使用Java(IDE-Eclipse)中的RELAX NG Schema验证xml文件](/upload/InfoBanner/zyjiaocheng/673/2dae1eb6907f4731ac669cc74327d078.jpg)
我一直在尝试针对名为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),以便可以进行适当的验证.谢谢您的期待.
解决方法:
通过SchemaFactory进行RELAX NG验证不需要Java实现.因此,即使它在一种环境中工作,它也不是便携式的.从错误消息中,您的特定Java实现似乎不支持它.
由于您具有Jing库,因此可以验证是否使用它们-请参阅文档here以开始使用.
内容总结
以上是互联网集市为您收集整理的使用Java(IDE-Eclipse)中的RELAX NG Schema验证xml文件全部内容,希望文章能够帮你解决使用Java(IDE-Eclipse)中的RELAX NG Schema验证xml文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。