tags:

views:

224

answers:

3

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 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"&gt;
    <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>
+1  A: 

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.

You already have an XML schema, so you probably want:

<!DOCTYPE schema PUBLIC "http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;

If you want to turn off validation, you need something like:

spf.setValidating(false); (Where spf is a SaxParserFactory)

Benj
so how do I do set the doctype or turn off the validation?
Marcos Roriz
so I need to store the dtd locally?
Marcos Roriz
I've edited my answer to show how you can turn validation off. You can use a URL rather than a file reference for the DTD if you want to use one.
Benj
Actually, when I looked at your XML again, you already have a scaema referenced so you can probably set the doctype to use that (see above).
Benj
DTD is a bit obselete. Furthermore, jpa uses XSD
Bozho
but where do I put this dtd, in the xml file?
Marcos Roriz
No just use <!DOCTYPE schema PUBLIC "http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">I think that should do it.
Benj
(Get that from the answer it's self, it all went a bit wrong when I copied it into the comment)
Benj
You might want to read a bit about doctype declarations before giving advice: http://www.w3.org/TR/REC-xml/#NT-doctypedecl ... your "DOCTYPE schema" doesn't say "use this schema", it says "expect a root element named 'schema' and use this referenced doctype"
kdgregory
A: 

The XML document defines the default namespace http://java.sun.com/xml/ns/persistence and includes a url, where the schema can be found (xsi:schemaLocation attribute, first value is the namespace, second the url or path).

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:schemaLocation value.

Andreas_D
A: 

The exception says that an ErrorHandler wasn'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 ErrorHandler implementation and attach it to the 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).

Or, as other responses have suggested, you can just turn validation off.

kdgregory