【问题标题】:Validating an xml file using RELAX NG Schema in Java (IDE - Eclipse)在 Java (IDE - Eclipse) 中使用 RELAX NG Schema 验证 xml 文件
【发布时间】: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 也可以),以便可以完成正确的验证。感谢期待。

【问题讨论】:

  • 看起来该代码的一半与您提出的问题无关。请更新您的问题以将其删除。

标签: java xml


【解决方案1】:

我遇到了同样的问题,结果发现我在类路径中缺少 jing-20091111.jar。

我一直在使用一些类加载器机制,所以如果我在我的代码中使用它们,所有的 jing 类都是可用的。问题是 SchemaFactory 不知道我的类加载器,所以我不得不将 jar 直接放在类路径中。

所以我认为 alexbrn 关于特定 Java 实现支持的回应是错误的。当 System.setProperty() 用于为 RELAX NG 提供实现时,它应该可以在每个 JVM 中工作。

【讨论】:

    【解决方案2】:

    Java 实现不需要通过 SchemaFactory 实现 RELAX NG 验证。因此,即使它在一种环境中工作,它也不是便携式的。从您的错误消息来看,您的特定 Java 实现似乎不支持它。

    由于您拥有 Jing 库,因此您可以使用它们进行验证 - 请参阅文档 here 以开始使用。

    【讨论】:

    • 使用 Jing API 中的 ValidationDriver 解决了这个问题。干杯!
    • 谢谢 alexbrn,非常感谢。该文档确实有效。
    • Jing 和 RELAXNG ......他们......他们只是工作!非常感谢你,让我知道他们的存在!我终于可以依赖我的 XML 文件了...
    猜你喜欢
    • 2013-06-01
    • 2010-12-05
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多