HierarchicalXMLConfiguration Example

The following is hopefully a helpful example of how to use the HierarchicalXMLConfiguration class as there appears to be a complete lack of doco on the jakarta site.

Note: updates to a HierarchicalXMLConfiguration cannot be saved as it is a readonly Configuration (as at commons configuration 1.0). Note Note: it appears that the save method has been implemented and is likely to appear in the 1.1 release.

The example uses the following xml file and demonstrates how to read the configuration details from it.

<?xml version="1.0" encoding="ISO-8859-1" ?>

<Repositories>
        <Repository>
                <workspaceName>svn://nicole/doc</workspaceName>
                <type>Subversion</type>
                <username></username>
                <password></password>
        </Repository>
        <Repository>
                <workspaceName>svn://nicole/doc/test</workspaceName>
                <type>Subversion</type>
                <username></username>
                <password></password>
        </Repository>
        <Repository>
                <workspaceName>svn://nicole/doc/test/3</workspaceName>
                <type>Subversion</type>
                <username></username>
                <password></password>
        </Repository>
</Repositories>

The following code reads the details of each repository entry in turn.

        static public void main(String[] args)
        {
                try
                {
                        HierarchicalXMLConfiguration conf = new HierarchicalXMLConfiguration();
                        conf.load(new File(".", "repositories.xml"););

                        int nRepositories = conf.getMaxIndex("Repository");
                        for (int i = 0; i <= nRepositories; i++)
                        {
                                String instance = "Repository(" + i + ")";
                                String type = conf.getString(instance + ".type");
                                String workspaceName = conf.getString(instance + ".workspaceName");
                                String username = conf.getString(instance + ".username");
                                char[] password = null;
                                String temp = conf.getString(instance + ".password");
                                if (temp != null)
                                        password = temp.toCharArray();

                                // Dump details
                                out.println("workspaceName = " + workspaceName);
                                out.println("type = " + type);
                                out.println("username = " + username);
                                if (password != null)
                                        out.println("password = " + new String(password));
                        }
                }
                catch (ConfigurationException e)
                {
                        System.out.println("ERROR: " + e.toString());
                }
        }

HierarchicalXMLConfiguration_Example (last edited 2009-09-20 23:48:15 by localhost)