Date: 2004-12-29T17:14:44
   Editor: BrettSutton
   Wiki: Jakarta Commons Wiki
   Page: HierarchicalXMLConfiguration Example
   URL: http://wiki.apache.org/jakarta-commons/HierarchicalXMLConfiguration 
Example

   no comment

New Page:

== 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.

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());
                }
        }
}}}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to