XMLConfiguration.setDefaultExpressionEngine(new XPathExpressionEngine());
config.setExpressionEngine(new XPathExpressionEngine());
XMLConfiguration config = new XMLConfiguration(filePath); config.setExpressionEngine(new XPathExpressionEngine());
/** * This default implementation constructs an empty {@link XMLConfiguration} with an {@link XPathExpressionEngine} * and a UTF-8 encoding. Note that when using this template generation method, the keys of the configuration * must be given in XPath form, i.e. 'key/subkey' instead of the default dotted notation 'key.subkey'. * @return An empty XMLConfiguration template. * @throws ConfigurationException If the template generation fails. */ default HierarchicalConfiguration<ImmutableNode> createConfigurationTemplate() throws ConfigurationException { Parameters params = new Parameters(); FileBasedConfigurationBuilder<XMLConfiguration> builder = new FileBasedConfigurationBuilder<XMLConfiguration>(XMLConfiguration.class) .configure(params.xml() .setExpressionEngine(new XPathExpressionEngine()) .setEncoding(StandardCharsets.UTF_8.name()) ); XMLConfiguration c; try { c = builder.getConfiguration(); exposeParameters("", c); } catch (org.apache.commons.configuration2.ex.ConfigurationException e) { throw new ConfigurationException(); } return c; }
/** * Loads the Apache Commons Configuration2 {@link XMLConfiguration} from the given file. By default, * the {@link XPathExpressionEngine} is set. * @param configurationFile * @return * @throws ConfigurationException */ public static XMLConfiguration loadXmlConfiguration(File configurationFile) throws ConfigurationException { try { Parameters params = new Parameters(); FileBasedConfigurationBuilder<XMLConfiguration> configBuilder = new FileBasedConfigurationBuilder<>(XMLConfiguration.class).configure(params .xml() .setExpressionEngine(new XPathExpressionEngine()) .setFile(configurationFile)); return configBuilder.getConfiguration(); } catch (org.apache.commons.configuration2.ex.ConfigurationException e) { throw new ConfigurationException(e); } }
XMLConfiguration config = new XMLConfiguration("games.xml"); String gameName = config.getString("game1.name"); List<Object> playerNames = config.getList("game1.players.player.name"); // ... config.setProperty("game1.name", "Space Invaders"); // update game name config.addProperty("game1.players.player(-1).name", "ted"); // add new players config.addProperty("game1.players.player(-1).name", "carol"); config.clearTree("game1.players.player(1)"); // remove a player // Or with XPath config.setExpressionEngine(new XPathExpressionEngine()); config.addProperty("game1/players player/name", "ted"); config.addProperty("game1/players player/name", "carol"); config.clearTree("game1/players/player[2]");