JDK-7099658 : Properties.loadFromXML fails with ClassCastException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 6u22
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2011-10-11
  • Updated: 2014-11-19
  • Resolved: 2011-11-11
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availabitlity Release.

To download the current JDK release, click here.
JDK 6 JDK 7 JDK 8
6u29-rev b22Fixed 7u4Resolved 8Resolved
Description
SHORT SUMMARY:
Classcast exception when using non-default JAXP parser 
INDICATORS:
Exception : 
Exception in thread "main" java.lang.ClassCastException:
oracle.xml.parser.v2.DTD cannot be cast to org.w3c.dom.Element
at java.util.XMLUtils.load(XMLUtils.java:61) 

COUNTER INDICATORS:
TRIGGERS:
Use of non-default JAXP parser (oracle xml parser xmlparserv2.jar) in this 
case while making calls into java.util.XMLUtils.load method
KNOWN WORKAROUND:
Modify the parser used.
PRESENT SINCE:
N/A
HOW TO VERIFY:
Testcase presented in BugDB report. oracle xml parser "xmlparserv2.jar" 
should be present in CLASSPATH before running testcase.
NOTES FOR SE:
Logged on jdk 6u22 with linux x86 platform

Fix is simple and suggested by submitter : 

==
The error is in the line:

Element propertiesElement = (Element)doc.getChildNodes().item(1);

which is making an incorrect assumption about the in-memory structure
of the parsed XML document. Instead, it should access the document
element of the XML DOM tree using the standard DOM API getDocumentElement():

Element propertiesElement = doc.getDocumentElement(); 
----
REGRESSION:
No

Comments
EVALUATION simple fix is to use the standard parser API call as suggested.
2011-10-11