简体   繁体   English

使用 Java 中的 RELAX NG 模式验证 xml 文件(IDE - Eclipse)

[英]Validating an xml file using RELAX NG Schema in Java (IDE - Eclipse)

I have been trying to validate an xml file name bookNew.xml against an.rnc file named bookNewRelax.rnc.我一直在尝试针对名为 bookNewRelax.rnc 的.rnc 文件验证 xml 文件名 bookNew.xml。

The error that I constantly face is --我经常面临的错误是——

Exception in thread "main" java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://relaxng.org/ns/structure/1.0 could be loaded at javax.xml.validation.SchemaFactory.newInstance(Unknown Source) at testRelax.main(testRelax.java:38) Exception in thread "main" java.lang.IllegalArgumentException: No SchemaFactory that implements the schema language specified by: http://relaxng.org/ns/structure/1.0 could be loaded at javax.xml.validation.SchemaFactory.newInstance(Unknown来源)在 testRelax.main(testRelax.java:38)

In order to prevent this, I used a line of code before instantiating an object of the SchemaFactory class, which I believed would help solve this issue.为了防止这种情况,我在实例化 SchemaFactory class 的 object 之前使用了一行代码,我相信这将有助于解决这个问题。 the ptece of code is as under:-代码的ptece如下:-

System.setProperty(SchemaFactory.class.getName() + ":" + XMLConstants.RELAXNG_NS_URI, "com.thaiopensource.relaxng.jaxp.CompactSyntaxSchemaFactory"); 
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.RELAXNG_NS_URI); 

I have included the external jar - jing.jar in my project and still, the same exception is being thrown.我已经在我的项目中包含了外部 jar - jing.jar,但仍然抛出了同样的异常。

I have also imported the library com.thaiopensource.*;我还导入了库 com.thaiopensource.*; and it is underlined in yellow showing that it is never used at all.它带有黄色下划线,表明它根本没有使用过。 Personally, I think, it is the jar file playing spoilsport here, else why would the thaiopensource library be never come into use.就个人而言,我认为,这是 jar 文件在这里玩破坏,否则为什么 thaiopensource 库永远不会被使用。

I am pasting the java file underneath.我在下面粘贴 java 文件。

import java.io.*;进口 java.io.*; import java.lang.management.ManagementFactory;导入 java.lang.management.ManagementFactory; import java.lang.management.ThreadMXBean;导入 java.lang.management.ThreadMXBean;

import javax.xml.XMLConstants;导入 javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder;导入 javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory;导入 javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException;导入 javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.dom.DOMSource;导入 javax.xml.transform.dom.DOMSource; import javax.xml.validation.*;导入 javax.xml.validation.*;

import org.w3c.dom.Document;导入 org.w3c.dom.Document; import org.xml.sax.SAXException;导入 org.xml.sax.SAXException;

import com.thaiopensource.*;导入 com.thaiopensource.*;

public class testRelax {公共 class 测试放松 {

/** 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());
    }
}

} }

Kindly advise me how to make my java program "accept" the RELAX NG Compact Schema (or else simply.rng will also do) so that the proper validation may be done.请告诉我如何让我的 java 程序“接受”RELAX NG Compact Schema(或者也可以简单地使用.rng),以便可以完成正确的验证。 Thanks in anticipation.感谢期待。

Java implementations are not required to implement RELAX NG validation via SchemaFactory. Java 实现不需要通过 SchemaFactory 实现 RELAX NG 验证。 So even if it works in one environment, it is not portable.因此,即使它在一种环境中工作,它也不是便携式的。 From your error message, it appears your particular Java implementation doesn't support it.从您的错误消息中,您的特定 Java 实现似乎不支持它。

Since you have the Jing libraries, you can validate using them - see the documentation here to get started.由于您拥有 Jing 库,因此您可以使用它们进行验证 - 请参阅此处的文档以开始使用。

I had the same problem and it turned out that I was missing jing-20091111.jar from the classpath.我遇到了同样的问题,结果发现我在类路径中缺少 jing-20091111.jar。

I've been using some class loader mechanisms, so all the jing classes were available if I used them in my code.我一直在使用一些 class 加载器机制,所以如果我在我的代码中使用它们,所有的 jing 类都是可用的。 The problem was that SchemaFactory didn't know about my classloaders, so I had to put the jar directly in the classpath.问题是 SchemaFactory 不知道我的类加载器,所以我不得不将 jar 直接放在类路径中。

So I think alexbrn's response about particular Java implementations' support is wrong.所以我认为 alexbrn 对特定 Java 实现的支持的回应是错误的。 When System.setProperty() is used to provide implementation for RELAX NG, it should work in every JVM.当 System.setProperty() 用于为 RELAX NG 提供实现时,它应该在每个 JVM 中工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM