[jdom-interest] Getting attribute name/value pairs

Frank Cohen fcohen at inclusion.net
Wed Jul 19 08:31:30 PDT 2000

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. 

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;

I get a java.lang.ClassCastException: org.jdom.Element

Any idea what I'm doing wrong?




  <!-- =================================================================== -->
  <!-- Initialization 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"/>



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
        at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(Abstra
        at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel
        at javax.swing.JToggleButton$ToggleButtonModel.setPressed(Compiled Code)

        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonL

  public void start()
    // Load the entire document into memory
    // from the network or file system
      // 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;


// 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");}




       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() );
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20000719/d2f6faee/attachment.htm

More information about the jdom-interest mailing list