4

I'm working on a class to load an XML file configuration and this class extends from XMLConfiguration.

The configuration file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <global>
        <reloadInterval>5</reloadInterval>
    </global>

    <definitions>
        <definition>
            <id>1</id>
            <name>Test name</name>
            <messages>
                <message id="1">Help menu opt 1</message>
                <message id="2">Help menu opt 2</message>
                <message id="3">Help menu opt 3</message>
            </messages>
        </definition>
    </definitions>
</root>

The way I'm iterating this and loading is as follows:

   private void updateDefinitions()
   {
      this.definitions.clear();

      List<SubnodeConfiguration> lstDefinitions = getListConfig("definitions");
      for(SubnodeConfiguration definition : lstDefinitions)
      {
         DefinitionBean aDefinition = new DefinitionBean();

         aDefinition.setId(definition.getInt("Definition.id", -1));
         aDefinition.setName(definition.getString("Definition.name", ""));

         List<MessageBean> messages = new ArrayList<MessageBean>();
         List<SubnodeConfiguration> lstMessages = definition.configurationsAt("definition.messages");
         for(SubnodeConfiguration messageBean : lstMessages)
         {
            MessageBean message = new MessageBean();

            message.setId(messageBean.getString("message"));
            messages.add(message);
         }

         definition.setMessages(messages);
         this.definitions.put(aDefinition.getId(), aDefinition);

      }
   }

The code is working fine, however, the problem is to get the id attribute for each <message> element, I don't know how to retrieve it. None of the getters for SubnodeConfiguration provides this, or maybe, I'm not doing it in the correct way.

Any help will be appreciated.

1
  • Ok, I've found it. How did I found it, I've iterated through the getKeys() enumerator as follows: Iterator iter = messageBean.getKeys(); Object value; while (iter.hasNext()) { value = (iter.next()); System.out.println(value.toString()); } Doing this, I've noticed that to access the attribute "id", I have to do it as follows: messageBean.getString("message[@id]") Hope this helps if somebody is looking for it. Fer Mar 13, 2012 at 16:57

1 Answer 1

6

I see your comment, but it's also in the documentation here. SubnodeConfiguration extends HierarchicalConfiguration.

Additionally, if you retrieve the Configuration using the method HierarchicalConfiguration.configurationsAt(), and the node you're retrieving, itself has attributes, you just access it with the square bracket + @ notation. For example:

Xml:

<foos>
  <foo bar='bazz'/>
  <foo bar='bizz'/>
  <foo bar='buzz'/>
</foos>

Java:

// load xml into config
XmlConfiguration config = ...
List<HierarchicalConfiguration> foos = config.configurationsAt("foo");
for (HierarchicalConfiguration foo : foos) {
    System.out.println(foo.getString("[@bar]"));
}

Should print:

bazz
bizz
buzz
3
  • If I don't know the 'foo' key's name, how can I get all the 'bar' attibute's values.
    – Liam Mazy
    Dec 18, 2015 at 8:20
  • @jetma Dunno. 3.5 years is too long to recall anything for this. JavaDoc should tell you, or you can just debug and play. :-) Dec 25, 2015 at 15:48
  • 4
    Just a note for anybody like me who might have automatically assumed that the intended expression engine was XPath here - it's not. If you're using the XPath engine, you'll need to omit the brackets around the attribute name.
    – SPoage
    Mar 2, 2016 at 21:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.