[英]Validating an xml file using RELAX NG Schema in Java (IDE - Eclipse)
我一直在嘗試針對名為 bookNewRelax.rnc 的.rnc 文件驗證 xml 文件名 bookNew.xml。
我經常面臨的錯誤是——
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)
為了防止這種情況,我在實例化 SchemaFactory class 的 object 之前使用了一行代碼,我相信這將有助於解決這個問題。 代碼的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.*;
公共 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());
}
}
}
請告訴我如何讓我的 java 程序“接受”RELAX NG Compact Schema(或者也可以簡單地使用.rng),以便可以完成正確的驗證。 感謝期待。
Java 實現不需要通過 SchemaFactory 實現 RELAX NG 驗證。 因此,即使它在一種環境中工作,它也不是便攜式的。 從您的錯誤消息中,您的特定 Java 實現似乎不支持它。
由於您擁有 Jing 庫,因此您可以使用它們進行驗證 - 請參閱此處的文檔以開始使用。
我遇到了同樣的問題,結果發現我在類路徑中缺少 jing-20091111.jar。
我一直在使用一些 class 加載器機制,所以如果我在我的代碼中使用它們,所有的 jing 類都是可用的。 問題是 SchemaFactory 不知道我的類加載器,所以我不得不將 jar 直接放在類路徑中。
所以我認為 alexbrn 對特定 Java 實現的支持的回應是錯誤的。 當 System.setProperty() 用於為 RELAX NG 提供實現時,它應該在每個 JVM 中工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.