<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=iso-8859-1" http-equiv=Content-Type>
<META content="MSHTML 5.00.2920.0" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>I'm using JDOM Beta 4 with JDK 122 from Sun. I'm
rewriting an application to use XML-coded property files. The property file
contains a list of property elements with simple attribute name=value pairs.
</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>I am having problems using the getAttributes()
method. My code looks for the <load_init_values> element, a list of
property elements is containined within. I get a list of the property
elements and iterate through the list. When I find a property element in the
list I ask for the attributes using getAttributes(). This gives me a List. When
I iterate through the list I find the list is composed of Elements, not
Attributes. So when I do:</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> Object q = iterator.next();
<DIV><FONT face=Arial size=2> Attribute r = (Attribute)
q;</FONT></DIV></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>I get a java.lang.ClassCastException:
org.jdom.Element</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Any idea what I'm doing wrong?</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>-Frank</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>--</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><load></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> <!--
===================================================================
--><BR> <!-- Initialization
values
--><BR> <!--
===================================================================
--></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> <load_init_values></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> <echo message="Starting Load
2.0 alpha 1"/></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> <property
name="scripts_directory" value="./scripts"/><BR>
<property name="accept_script_versions" value="2"/></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> <property name="log.file"
value="./load.log"/><BR> <property name="log.append"
value="true"/><BR> <property name="log.level"
value="6"/></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> <property name="url"
value=""/><BR> <property name="cookieok"
value="true"/><BR> <property name="bad_string"
value=""/><BR> <property name="timeout"
value="0"/><BR> <property name="sleeptime"
value="0"/><BR> <property name="threadcount"
value="0"/></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> </load_init_values></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2></load></FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>Exception occurred during event
dispatching:<BR>java.lang.ClassCastException:
org.jdom.Element<BR> at
load.commander.start(commander.java:159)<BR>
at
load.loadui.buttonRun_actionPerformed(loadui.java:208)<BR>
at
load.loadui$4.actionPerformed(loadui.java:124)<BR>
at
javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:10<BR>66)<BR>
at
javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra<BR>ctButton.java:1101)<BR>
at
javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel<BR>.java:378)<BR>
at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Compiled
Code)</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> at
javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL<BR>istener.java:217)</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2> public void start()<BR>
{<BR> // Load the entire document into
memory<BR> // from the network or file
system<BR> try<BR>
{<BR> // Gets the Load settings file, parses it
into a DOM tree<BR> Document doc =
load_info_builder.build( default_settings );</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> // If there are no
well-formedness errors,<BR> // then no exception
is thrown<BR> uiframe.show( default_settings + "
is well formed.");</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> // Only read Load setting
files<BR> Element load_set =
doc.getRootElement();<BR> if (
load_set.getName().equals("load") )<BR>
{<BR> // This is a load setting file so parse its
contents</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> loadui.show(
default_settings + " parsing contents");</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> //
Within the load element is the load_init_values
group<BR> // These are the elements we
want to set the defaults for Load<BR>
List children =
load_set.getChild("load_init_values").getMixedContent();</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> Iterator
iterator = children.iterator();<BR>
while (iterator.hasNext())<BR>
{<BR> Object o =
iterator.next();</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2> if (o instanceof
Element)<BR>
{<BR> Element
p = (Element) o;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2> //
echo elements are comments that get displayed in the
UI<BR> if (
p.getName().equals("echo")
)<BR>
{<BR>
echo( p.getAttribute("message").getValue()
);<BR>
}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2> //
property elements are simple name = value pairs that set
a<BR> //
public variable in the commander
object<BR> if (
p.getName().equals("property")
)<BR>
{</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
List property_list =
p.getAttributes();<BR>
Iterator property_iterator = property_list.iterator();</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
echo( property_list.size() + " = property list size");</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
while ( property_iterator.hasNext()
)<BR>
{<BR>
Object q = iterator.next();</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// The problem seems to happen here. I get a
runtime exception complaining that Java couldn't</FONT></DIV>
<DIV><FONT face=Arial size=2>// handle the ClassCast.</FONT></DIV>
<DIV> </DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
Attribute r = (Attribute) q;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
echo(r.getName());<BR>
echo(r.getValue());</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>// I added these if statements to see what kind of
object Java thinks the Attribute class actuall is</FONT></DIV>
<DIV><FONT face=Arial size=2>// It turns out that q is always an instance of
Element, not Attribute.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
if (q instanceof Attribute) {
echo("attribute");}<BR>
if (q instanceof Element) { echo("element> " + ((Element) q).getName()
);}<BR>
if (q instanceof String) { echo("string");}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial
size=2>
}<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
}<BR> else<BR>
{<BR> loadui.show( default_settings + "does
not appear to be a Load settings file");<BR>
}<BR> }<BR> catch (JDOMException e) { //
indicates a well-formedness or other error<BR>
uiframe.show( default_settings + " is not well
formed.");<BR> uiframe.show( e.getMessage()
);<BR> }<BR> }<BR>}</FONT></DIV></BODY></HTML>