java 警告:验证已打开,但 org.xml.sax.ErrorHandler
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1866886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: validation was turned on but an org.xml.sax.ErrorHandler
提问by Marcos Roriz Junior
Any idea why this error is happening and how to fix it? I'm getting this error when trying to parse/load a config file:
知道为什么会发生此错误以及如何修复它吗?尝试解析/加载配置文件时出现此错误:
Error
错误
Warning: validation was turned on but an org.xml.sax.ErrorHandler was not
set, which is probably not what is desired. Parser will use a default
ErrorHandler to print the first 10 errors. Please call
the 'setErrorHandler' method to fix this.
Error: URI=null Line=3: Document root element "persistence", must match DOCTYPE root "null".
Error: URI=null Line=3: Document is invalid: no grammar found.
null
[]
null
Main code
主要代码
public static void main(String[] args) throws ConfigurationException {
config = new XMLPropertiesConfiguration(new File("META-INF/vamola.xml"));
System.out.println(config.getString("persitence-unit.provider"));
System.out.println(config.getList("persistence-unit.properties.name"));
}
XML FILE
XML文件
<?xml version="1.0"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="dbBank" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>br.ufg.inf.server.Account</class>
<class>br.ufg.inf.server.UserBank</class>
<properties>
<property name="toplink.jdbc.user" value="derby" />
<property name="toplink.jdbc.password" value="senha" />
<property name="toplink.jdbc.url" value="jdbc:derby://192.168.80.125:1527/db/master/dbBank;create=true"/>
<property name="toplink.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
<property name="toplink.ddl-generation" value="create-tables" />
<property name="toplink.logging.level" value="OFF" />
<property name="toplink.target-database" value="Derby" />
</properties>
</persistence-unit>
</persistence>
回答by Benj
If your parsing an XML document with validation turned on, you need to specify either a DTD or an XML schema in a DOCTYPE at the start of your XML document. Your parser is basically complaining that it doesn't know how to validate your document because no grammer has been specified to validate the mark up.
如果在打开验证的情况下解析 XML 文档,则需要在 XML 文档开头的 DOCTYPE 中指定 DTD 或 XML 模式。您的解析器基本上是在抱怨它不知道如何验证您的文档,因为没有指定语法来验证标记。
You already have an XML schema, so you probably want:
您已经有一个 XML 模式,因此您可能需要:
<!DOCTYPE schema PUBLIC "http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
If you want to turn off validation, you need something like:
如果要关闭验证,则需要以下内容:
spf.setValidating(false);(Where spf is a SaxParserFactory)
spf.setValidating(false);(其中 spf 是 SaxParserFactory)
回答by kdgregory
The exception says that an ErrorHandlerwasn't set. This means that the parser uses its built-in error handler, which simply writes messages to the console. If you actually want to validate, you need to create an ErrorHandlerimplementation and attach it to the DocumentBuilder.
异常说 anErrorHandler没有设置。这意味着解析器使用其内置的错误处理程序,它只是将消息写入控制台。如果您确实想要验证,则需要创建一个ErrorHandler实现并将其附加到DocumentBuilder.
For more information, read this: http://www.kdgregory.com/index.php?page=xml.parsing(error handlers are about 1/3 of the way down).
有关更多信息,请阅读:http://www.kdgregory.com/index.php?page =xml.parsing(错误处理程序大约下降了 1/3)。
Or, as other responses have suggested, you can just turn validation off.
或者,正如其他回复所建议的那样,您可以关闭验证。
回答by Andreas Dolk
The XML document defines the default namespace http://java.sun.com/xml/ns/persistenceand includes a url, where the schema can be found (xsi:schemaLocationattribute, first value is the namespace, second the url or path).
XML 文档定义了默认命名空间http://java.sun.com/xml/ns/persistence并包含一个 url,可以在其中找到架构(xsi:schemaLocation属性,第一个值是命名空间,第二个值是 url 或路径)。
Please double-check if this url is accessible at the time you parse it. An alternative is to download the schema, put it on the file system and modify the xsi:schemaLocationvalue.
请仔细检查在您解析该网址时是否可以访问该网址。另一种方法是下载模式,将其放在文件系统上并修改xsi:schemaLocation值。

