3

When saving a configuration file, I need to specify the xmlns for the root element (by default <configuration>). The reason is, I validate (with a schema) upon loading the configuration file later.

I can't seem to find how commons-configuration exposes this functionalities. Any ideas? Thanks!

Miquel
  • 15,405
  • 8
  • 54
  • 87
  • 1
    There is a [`setRootNode(ConfigurationNode rootNode)`](http://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration/HierarchicalConfiguration.html#setRootNode(org.apache.commons.configuration.tree.ConfigurationNode)) that might work. You can set an attribute (xmlns in this case) on the [`ConfigurationNode`](http://commons.apache.org/proper/commons-configuration/apidocs/org/apache/commons/configuration/tree/ConfigurationNode.html). – maba Apr 12 '13 at 12:45

2 Answers2

3

There is a setRootNode(ConfigurationNode rootNode) that might work. You can set an attribute (xmlns in this case) on the ConfigurationNode.

maba
  • 47,113
  • 10
  • 108
  • 118
3

The suggestion by @maba works. It's not pretty, but there doesn't seem to be a more elegant way to do this. Here's the code:

private void setNameSpace(XMLConfiguration conf) {
    HierarchicalConfiguration.Node root = new HierarchicalConfiguration.Node(
            "configuration");
    root.addAttribute(new HierarchicalConfiguration.Node("xmlns",
            "http://namespace.com"));
    conf.setRootNode(root);
}

Where the relevant imports are:

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.configuration.XMLConfiguration;
Miquel
  • 15,405
  • 8
  • 54
  • 87