Search code examples
javaxmldtdsaxparsersaxparseexception

why am I getting SAXparseException "Element type must be declared" eventhough it was declared while loading XML file to Properties Object?


I am a newbie to java and eclipse. I was trying to load an XML file using properties.loadFromXML() and tried to read properties's names. but I ended with getting SAXparseException saying "Element type must be declared", even though I have defined DTD for my XML file. Could someone help me? this is my XML file

<?xml version="1.0"?>
<!DOCTYPE test [
<!ELEMENT test (name, price, hsn)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT hsn (#PCDATA)>
]>
<test>
    <name>groundnut</name>
    <price>5.00</price>
    <hsn>ABCDE</hsn>
</test>

this is my java code: import java.util.Properties; import java.util.Set;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.io.File;
import java.io.IOException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import java.io.*;


public class test 
{
    public static void main(String args[]) throws Exception
    {
        Properties p = new Properties();
        InputStream is = new FileInputStream("resources/test.XML");
        p.loadFromXML(is);
        System.out.print(p.stringPropertyNames());

    }
}

this is the output I got:

Exception in thread "main" java.util.InvalidPropertiesFormatException: jdk.internal.org.xml.sax.SAXParseException; Element type "test" must be declared.
at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.load(PropertiesDefaultHandler.java:85)
at java.base/java.util.Properties.loadFromXML(Properties.java:956)
at test.main(test.java:28)

Caused by: jdk.internal.org.xml.sax.SAXParseException; Element type "test" must be declared. at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.startElement(PropertiesDefaultHandler.java:169) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:470) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:411) at java.base/jdk.internal.util.xml.impl.ParserSAX.parse(ParserSAX.java:374) at java.base/jdk.internal.util.xml.impl.SAXParserImpl.parse(SAXParserImpl.java:97) at java.base/jdk.internal.util.xml.PropertiesDefaultHandler.load(PropertiesDefaultHandler.java:83) ... 2 more


Solution

  • Importing XML using Properties.loadFromXML() does not support the use of custom DTDs. It only supports the DTD documented in the Properties Javadoc.

    It's not clear why you think you can use an alternative DTD to read in properties like this.

    You will instead have to modify your XML document to match the Properties DTD:

    <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
    <properties>
        <entry key="name">groundnut</entry>
        <entry key="price">5.00</entry>
        <entry key="hsn">ABCDE</entry>
    </properties>