J2SE versions prior to 1.5 required you to work directly with the XML parser to load a configuration file and store settings. While it was never difficult, and while the parser is a standard part of the platform, the extra work was a bit of an annoyance. The newly updated java.util.Properties
class now offers an easier way to load and store settings for a program: the loadFromXML(InputStream is)
and storeToXML(OutputStream os, String comment)
methods.
If you aren't familiar with the java.util.Properties
class, you use it to store a set of key-value pairs in a file, where the key and value are separated by an equal sign, as shown in Listing 1.
Listing 1. Sample set of properties
foo=bar |
Had Listing 1 been loaded into a Properties
object, you would then find two keys (foo
and fu
) and two values (bar
for foo
and baz
for fu
). The class supports embedding Unicode strings with \u
, but the important thing here is that everything is treated as a String
.
Listing 2 shows how to load the properties file and list its current set of keys and values. You just pass an InputStream
for the file to the load()
method, and each key-value pair is added to the Properties
instance. You would then use list()
to list all the properties or getProperty()
to retrieve an individual one.
Listing 2. Loading properties
import java.util.*; |
Running the LoadSample program produces the output in Listing 3. Notice that the output of the list()
method does not produce the list of key-value pairs in the same order they were in the input file. The Properties
class stores the set of pairs in a hashtable (in fact, it is a Hashtable
subclass), so there is no guarantee for order.
Listing 3. Output from LoadSample
-- listing properties -- |
None of this should be new to you. This is how the Properties
class has always worked. What is new, however, is the option of loading a set of properties from an XML file. The DTD for that is shown in Listing 4.
Listing 4. Properties DTD
<?xml version="1.0" encoding="UTF-8"?> |
If you're not into reading XML DTDs, this essentially says that wrapped within an outer
tag is a
tag, followed by any number of
tags. For each
tag, there is a key attribute, with the contents of the entry being its value. Listing 5 shows what the XML version of the properties file in Listing 1 would look like.
Listing 5. XML version of the Properties file
<?xml version="1.0" encoding="UTF-8"?> |
As Listing 6 shows, reading the XML version of the Properties
file isn't much different than reading the older style format.
Listing 6. Reading the XML Properties file
import java.util.*; The other side of the new Listing 7. Storing Properties as an XML file
The output produced from running the program in Listing 7 is shown in Listing 8. Listing 8. Stored XML file
The choice of using an XML file or older-style a=b type file is completely up to you. The older style is certainly lighter weight from a memory perspective. However, pervasiveness of XML, one would expect the XML format to be popular, as it is widely used already, just not by means of the |
0 comments:
Post a Comment