2011-04-11 111 views
4

我試圖根據XSD模式驗證某些XML輸入。解析器的工作原理,但是當我在模式中掛鉤時,我會在下面得到例外。我已經用外部驗證器驗證了測試輸入,所以我已經消除了這一點。有誰知道如何設置一個生成器來驗證這種方式?使用DocumentBuilder對模式進行驗證

以下是構建器設置代碼。

final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
factory.setIgnoringElementContentWhitespace(true); 
factory.setNamespaceAware(true); 
final SchemaFactory sf = 
     SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 

factory.setValidating(true); 
final Schema schema = sf.newSchema(new StreamSource(
     getClass().getResourceAsStream(SCHEMA_PATH))); 
factory.setSchema(schema); 

builder = factory.newDocumentBuilder(); 
builder.setErrorHandler(new DefaultErrorHandler()); 

而這是我得到的錯誤。

org.xml.sax.SAXParseException: Document is invalid: no grammar found. 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236) 
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382) 
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:177) 
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(XMLNSDocumentScannerImpl.java:779) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFragmentScannerImpl.java:1794) 
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:368) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834) 
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764) 
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148) 
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:250) 
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:292) 
    at com.casenet.external.mockclaims.ResponseScriptBuilder.build(ResponseScriptBuilder.java:54) 
    at com.casenet.external.mockclaims.ResponseScriptBuilderTest.buildFull(ResponseScriptBuilderTest.java:64) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:585) 
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:74) 
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:673) 
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:846) 
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1170) 
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) 
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109) 
    at org.testng.TestRunner.runWorkers(TestRunner.java:1125) 
    at org.testng.TestRunner.privateRun(TestRunner.java:749) 
    at org.testng.TestRunner.run(TestRunner.java:600) 
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:317) 
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:312) 
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:274) 
    at org.testng.SuiteRunner.run(SuiteRunner.java:223) 
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1007) 
    at org.testng.TestNG.runSuitesLocally(TestNG.java:932) 
    at org.testng.TestNG.run(TestNG.java:868) 
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:110) 
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:205) 
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:174) 
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:85) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:585) 
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:115) 
+0

請注意,架構是不可變的和線程安全的:你不必多次加載它,只是它緩存在一個靜態字段。 – 2012-12-12 10:03:32

回答

6

你並不需要調用:

setValidating(true); 

如果你做到以下幾點:

documentBuilderFactory.setSchema(schema); 

setSchema(Schema) javadocs

這種處理將生效即使是 如果isValida ting()方法返回 false。

+1

這也解決了我的問題,謝謝! – 2012-12-12 10:02:53