[jdom-interest] Getting attribute name/value pairs

Brett McLaughlin brett.mclaughlin at lutris.com
Wed Jul 19 09:08:26 PDT 2000


> Frank Cohen wrote:
> 
> I'm using JDOM Beta 4 with JDK 122 from Sun. I'm rewriting an

OK. First suggestion is to use the latest source drop, since we've had a
lot of changes since Beta 4.

> application to use XML-coded property files. The property file
> contains a list of property elements with simple attribute name=value
> pairs.
> 
> 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:
> 
>    Object q = iterator.next();
>    Attribute r = (Attribute) q;

Can I see all of your code? That would help.

Thanks,
Brett

> 
> I get a java.lang.ClassCastException: org.jdom.Element
> 
> Any idea what I'm doing wrong?
> 
> -Frank
> 
> --
> 
> <load>
> 
>   <!--
> ===================================================================
> -->
>   <!-- Initialization
> values                                               -->
>   <!--
> ===================================================================
> -->
> 
>   <load_init_values>
> 
>     <echo message="Starting Load 2.0 alpha 1"/>
> 
>     <property name="scripts_directory" value="./scripts"/>
>     <property name="accept_script_versions" value="2"/>
> 
>     <property name="log.file" value="./load.log"/>
>     <property name="log.append" value="true"/>
>     <property name="log.level" value="6"/>
> 
>     <property name="url" value=""/>
>     <property name="cookieok" value="true"/>
>     <property name="bad_string" value=""/>
>     <property name="timeout" value="0"/>
>     <property name="sleeptime" value="0"/>
>     <property name="threadcount" value="0"/>
> 
>   </load_init_values>
> 
> </load>
> 
> 
> 
> Exception occurred during event dispatching:
> java.lang.ClassCastException: org.jdom.Element
>         at load.commander.start(commander.java:159)
>         at load.loadui.buttonRun_actionPerformed(loadui.java:208)
>         at load.loadui$4.actionPerformed(loadui.java:124)
>         at
> javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:10
> 66)
>         at
> javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
> ctButton.java:1101)
>         at
> javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
> .java:378)
>         at
> javax.swing.JToggleButton$ToggleButtonModel.setPressed(Compiled Code)
> 
>         at
> javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL
> istener.java:217)
> 
> 
> 
>   public void start()
>   {
>     // Load the entire document into memory
>     // from the network or file system
>     try
>     {
>       // Gets the Load settings file, parses it into a DOM tree
>      Document doc = load_info_builder.build( default_settings );
> 
>       // If there are no well-formedness errors,
>       // then no exception is thrown
>       uiframe.show( default_settings + " is well formed.");
> 
>    // Only read Load setting files
>       Element load_set = doc.getRootElement();
>       if ( load_set.getName().equals("load") )
>       {
>     // This is a load setting file so parse its contents
> 
>        loadui.show( default_settings + " parsing contents");
> 
>         // Within the load element is the load_init_values group
>         // These are the elements we want to set the defaults for Load
>         List children =
> load_set.getChild("load_init_values").getMixedContent();
> 
>         Iterator iterator = children.iterator();
>         while (iterator.hasNext())
>         {
>           Object o = iterator.next();
> 
>           if (o instanceof Element)
>           {
>             Element p = (Element) o;
> 
>             // echo elements are comments that get displayed in the UI
>             if ( p.getName().equals("echo") )
>             {
>               echo( p.getAttribute("message").getValue() );
>             }
> 
>             // property elements are simple name = value pairs that
> set a
>             // public variable in the commander object
>       if ( p.getName().equals("property") )
>             {
> 
>               List property_list = p.getAttributes();
>               Iterator property_iterator = property_list.iterator();
> 
>               echo( property_list.size() + " = property list size");
> 
>               while ( property_iterator.hasNext() )
>               {
>                 Object q = iterator.next();
> 
> 
> // The problem seems to happen here. I get a runtime exception
> complaining that Java couldn't
> // handle the ClassCast.
> 
> 
>                 Attribute r = (Attribute) q;
> 
>                 echo(r.getName());
>                 echo(r.getValue());
> 
> 
> // I added these if statements to see what kind of object Java thinks
> the Attribute class actuall is
> // It turns out that q is always an instance of Element, not
> Attribute.
> 
> 
>                 if (q instanceof Attribute) { echo("attribute");}
>                 if (q instanceof Element) { echo("element> " +
> ((Element) q).getName() );}
>                 if (q instanceof String) { echo("string");}
> 
>               }
> 
>          }
> 
>           }
>         }
> 
>       }
>       else
>       {
>        loadui.show( default_settings + "does not appear to be a Load
> settings file");
>       }
>     }
>     catch (JDOMException e) { // indicates a well-formedness or other
> error
>       uiframe.show( default_settings + " is not well formed.");
>       uiframe.show( e.getMessage() );
>     }
>   }
> }

-- 
Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 
http://www.lutris.com
http://www.enhydra.org



More information about the jdom-interest mailing list