Tutorials : Simplify Your Java Configurations :

System Properties

You can read system properties files the same way you read properties files:
. . .
Configuration systemConfig = new SystemConfiguration();
String os = systemConfig.getString("os.name");
. . .
Since you're using the same interface, Configuration, the methods are the same.

Using XML Files

The next possibility to look into is how to get data from an XML file. Consider this file:
<?xml version="1.0" encoding="UTF-8"?>
<dvd>
  <title>Lord of the Rings: The Fellowship of the Ring</title>
  <length>178</length>
  <actor>Ian Holm</actor>
  <actor>Elijah Wood</actor>
  <actor>Ian McKellen</actor>
</dvd>
Again, it's very simple to read data from this source:
. . .
Configuration config = new XMLConfiguration("dvd.xml");
String title = config.getString("title");
. . .
You reference data with a straightforward dot-notation. Note that the name of the outer XML-element is not specified. But what if you want to obtain all the actors? Here's how:
. . .
String actor = config.getString("actor");
String[] actors = config.getStringArray("actor");
List actorsList = config.getList("actor"); 
// Alternative, gives a list of Strings 
. . .
You can even use more complex XML files. For example, suppose you have a collection of DVDs, like in Listing 2.

To reference the individual elements, you may use an index. To access attributes, use an XPath-like notation:

. . .
Configuration config = new XMLConfiguration("dvds.xml");
listProperties(config);

List titles = config.getList("dvd.title");
System.out.println();
System.out.println(titles);

String title = config.getString("dvd.title(0)");
System.out.println(title);

List x = config.getList("dvd[@id]");
System.out.println(x);

int length = config.getInt("dvd(1).length");
System.out.println(length);
. . .
Here's the output from this program:
*** Contents of configuration object:
org.apache.commons.configuration.XMLConfiguration
dvd.title=[Lord of the Rings: The Fellowship of the Ring, The Matrix, Amadeus]
dvd.length=[178, 136, 158]
dvd.actor=[Ian Holm, Elijah Wood, Ian McKellen, Keanu Reeves, 
  Laurence Fishburne, F. Murray Abraham, Tom Hulce, Elizabeth Berridge]
dvd[@id]=[A, B, C]

[Lord of the Rings: The Fellowship of the Ring, The Matrix, Amadeus]
Lord of the Rings: The Fellowship of the Ring
[A, B, C]
136

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.