gpt4 book ai didi

java - 使用 DocumentBuilder 验证模式

转载 作者:数据小太阳 更新时间:2023-10-29 02:03:14 26 4
gpt4 key购买 nike

我正在尝试根据 XSD 架构验证某些 XML 输入。解析器可以工作,但是当我 Hook 模式时,出现以下异常。我已经使用外部 validator 验证了测试输入,所以我已经消除了它。有谁知道如何设置一个构建器来验证这种方式?

这是构建器设置代码。

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)

最佳答案

你不需要调用:

setValidating(true);

如果您执行以下操作:

documentBuilderFactory.setSchema(schema);

来自setSchema(Schema) javadocs :

This processing will take effect even if the isValidating() method returns false.

关于java - 使用 DocumentBuilder 验证模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5622101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com