Category Archives: apache-commons-config

reading xml using apache commons

I have an xml configuration as below

<testconfig enabled="false">
    <testnode state="xyz" country="pqr">
        <childnode>
            <add name="location" value="qwerty"></add>
            <add name="make" value="all"></add>  
        </childnode>
    </testnode>
</testconfig>

Need to have a method to fetch values, say getValue("childnode", "location") should give "qwerty"

Also another method to fetch attrib values, say getAttribValue("testnode", "state") should return "xyz"

Could anyone please help with apache commons?

FileBasedConfigurationBuilder from Apache Commons Configuration2 not writing the configuration file

I need to write a simple configuration file using apache-commons-configuration but no matter what I try, it's not writing anything to the file.

This is how the file should look like

<config>
  <foo>bar</foo>
</config>

This is what I'm doing to write the foo configuration:

private static final String USER_CONFIGURATION_FILE_NAME = "config.xml";

private final Path configFilePath = Paths.get(System.getProperty("user.home"), ".myapp",
USER_CONFIGURATION_FILE_NAME);

private final FileBasedConfigurationBuilder<XMLConfiguration> configBuilder=
  new FileBasedConfigurationBuilder<>(XMLConfiguration.class)
      .configure(new Parameters().xml().setFile(configFilePath.toFile()));

/**
 * Sets the foo configuration to the given {@link String}
 * 
 * @param foo The configuration to be set up
 * @throws ConfigurationException If any error occur while setting the property on the
 *         configuration file
 */
public void setfoo(final String bar) throws ConfigurationException {
  checkNotNull(bar);
  final Configuration config = configBuilder.getConfiguration();

  config.setProperty("config.foo", bar);

  configBuilder.save();
}

/**
 * Retrieves the foo set up on the configuration file
 * 
 * @return The foo set up on the configuration file
 * @throws ConfigurationException If any error occur while setting the property on the
 *         configuration file
 * @throws NoSuchElementException If there is no foo set up
 */
public String getFoo() throws ConfigurationException {
  return configBuilder.getConfiguration().getString("config.foo");
}

Am I missing something? In Apache Commons Configuration - File-based Configurations I can't see any other information needed to set up the file, so I really don't know what I'm missing here.

How to get list of entries in properties file using Apache commons config?

In my project we have a properties file which has some entries as below:

#Data key entries
datakey001
datakey321
datakey451
someotherkey

In fact its not a key value pair, but a list of keys. Using java.util.Properties I was able to get this using Properties.stringPropertyNames().

Now we are migrating to Apache Commons Configuration and I could not find any feature in this library to get all these keys as I used to get previously using java.util.Properties.

Is there any way in apache commons config by which I can get all these keys without changing the the structure of the properties file?

EDIT: I have tried using Configuration.getKeys() as below, but the output is empty.

 Configuration propertiesConfig = new PropertiesConfiguration("C:\\proj\\myprops.properties");
 Iterator<String> it = propertiesConfig.getKeys();
 while(it.hasNext()) {
     System.out.println(it.next());
 }

Queries in Java Apache Commons Configuration

I have a XML configuration file that looks like

<columns>
    <column>
        <id>MyID</id>
    </column>
    <column>
        <id>MyID2</id>
        <notNull>true</notNull>
        <sorted>true</sorted>
    </column>
    <column>
        <id>MyID3</id>
        <notNull>false</notNull>
    </column>
</columns>

and so on.. First of all I am searching a way to get all columns. And then I want to iterate over them to get their seperate elements:

For the first column only the id-tag. for the second one the id, notNull and sorted....

How can I get this by using the Apache Commons Configuration (http://commons.apache.org/proper/commons-configuration/)?

Thanks for your help!!