How to get xml attribute from SubnodeConfiguration?

I am working on a class to load the configuration of an XML file, and this class is distributed 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> 

How do I repeat this and download the following:

  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 works fine, however the problem is to get the id attribute for each <message> element, I don’t know how to get it. None of the recipients for SubnodeConfiguration give this, or maybe I'm not doing it the right way.

Any help would be appreciated.

+4
source share
1 answer

I see your comment, but it is also in the documentation here . SubnodeConfiguration extends the hierarchical configuration.

In addition, if you retrieve the Configuration using the Hierarchical configuration method .configurationsAt () and the node you retrieve the attributes, you simply access it with the square bracket + @notation. For instance:

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

Must print:

 bazz bizz buzz 
+5
source

Source: https://habr.com/ru/post/1401199/


All Articles