警告:验证已打开,但 org.xml.sax.ErrorHandler

Mar*_*ior 2 java xml

知道为什么会发生此错误以及如何修复它吗?尝试解析/加载配置文件时出现此错误:

错误

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
Run Code Online (Sandbox Code Playgroud)

主要代码

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"));

    }
Run Code Online (Sandbox Code Playgroud)

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> 
Run Code Online (Sandbox Code Playgroud)

Ben*_*enj 5

如果在打开验证的情况下解析 XML 文档,则需要在 XML 文档开头的 DOCTYPE 中指定 DTD 或 XML 模式。您的解析器基本上是在抱怨它不知道如何验证您的文档,因为没有指定语法来验证标记。

您已经有一个 XML 模式,因此您可能需要:

<!DOCTYPE schema PUBLIC "http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
Run Code Online (Sandbox Code Playgroud)

如果要关闭验证,则需要以下内容:

spf.setValidating(false); (其中 spf 是 SaxParserFactory)