java schema 验证_使用Java(IDE-Eclipse)中的RELAX NG Schema验证xml文...

本文档描述了作者在Java(Eclipse环境下)使用RELAX NG Compact Syntax Schema验证XML文件时遇到的问题。具体表现为尝试加载模式语言实现时抛出IllegalArgumentException。作者尝试设置System属性并引入jing.jar,但问题依旧存在。代码示例展示了如何实例化SchemaFactory和进行验证的过程,以及在验证过程中捕获并打印异常信息。寻求解决方案。
摘要由CSDN通过智能技术生成

我一直在尝试针对名为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),以便可以进行适当的验证.谢谢您的期待.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值