Let j2se1.5 load properties from XML

Source: Internet
Author: User
Tags format object cdata key string version xml parser
The J2se|xml Properties class is no longer new, it has been in the early days of Java programming, and almost nothing has changed. The Tiger version of J2SE enhances this class, not only by specifying multiple key-value pairs separated by an equal sign in a single line, but also by loading and saving these key-value pairs in an XML file. In a series of tame Tiger articles, John Zukowski shows how to harness this new generation of "horse-riding".

The previous version of J2SE 1.5 required that the XML parser be used directly to mount the configuration file and store the settings. While this is not a difficult task, and the parser is the standard part of the platform, the extra work is always a bit annoying. The recently updated Java.util.Properties class now provides an easier way to set up and store your programs: LoadFromXML (InputStream is) and Storetoxml (OutputStream OS, String Comment) method.

   basic knowledge of Properties

If you are unfamiliar with the Java.util.Properties class, you are now told that it is used to store key-value pairs in a file where the keys and values are separated by an equal sign, as shown in Listing 1.

Listing 1. A set of properties examples

Foo=bar
Fu=baz


After you load listing 1 into the Properties object, you can find two keys (foo and FU) and two values (Foo's Bar and Fu's Baz). This class supports embedded Unicode strings with \u, but what is important here is that each item is treated as a string.

   now with the J2SE 1.5 Beta 1

Listing 2 shows how to load a property file and list its current set of keys and values. By simply passing the InputStream of this file to the load () method, each key-value pair is added to the Properties instance. The list () is then used to list all attributes or to obtain a separate attribute with GetProperty ().

Listing 2. Mount Properties

Import java.util.*;
Import java.io.*;

public class Loadsample {
public static void Main (String args[]) throws Exception {
Properties prop = new properties ();
FileInputStream fis = new FileInputStream ("Sample.properties");
Prop.load (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" +
Prop.getproperty ("foo"));
}
}


Run the Loadsample program to generate the output shown in Listing 3. Note the order of the key-value pairs in the output of the list () method is different from the order in which they are in the input file. The Properties class stores a set of key-value pairs in a hash table (hashtable, in fact, a Hashtable subclass), so there is no guarantee of order.

Listing 3. The output of the Loadsample

--Listing properties--
Fu=baz
Foo=bar

The Foo Property:bar


   XML Properties File

There is no new content here. The Properties class always works like this. The new place, however, is to load a set of attributes from an XML file. Its DTD is shown in Listing 4.

Listing 4. Property DTD

!--DTD for Properties-->
! ELEMENT Properties (Comment, entry*) >
! Attlist Properties Version CDATA #FIXED "1.0" >
! ELEMENT Comment (#PCDATA) >
! ELEMENT entry (#PCDATA) >
! Attlist entry key CDATA #REQUIRED >


If you do not want to read the XML DTD carefully, you can tell that it means that a <comment> label is wrapped in the perimeter <properties> label, followed by any number of <entry> labels. For each <entry> label, there is a key attribute, and the input is its value. Listing 5 shows what the XML version of the property file in Listing 1 looks like.

Listing 5. XML version of the property file

! DOCTYPE Properties SYSTEM "HTTP://JAVA.SUN.COM/DTD/PROPERTIES.DTD" >

Hi
bar
baz


As shown in Listing 6, reading the XML version of the Properties file is no different than reading the old format file.

Listing 6. Reading XML Properties File

Import java.util.*;
Import java.io.*;

public class Loadsamplexml {
public static void Main (String args[]) throws Exception {
Properties prop = new properties ();
FileInputStream fis = new FileInputStream ("Sampleprops.xml");
Prop.loadfromxml (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" +
Prop.getproperty ("foo"));
}
}


   a description of resource bindings

Although the Java.util.Properties class now supports the property file as an XML file in addition to supporting key-value pairs, unfortunately, there is no built-in option to handle ResourceBundle as an XML file. Yes, propertyResourceBundle does not use the Properties object to load bindings, but the use of the Mount method is hard-coded into the class rather than the newer LoadFromXML () method.

The program running listing 6 produces the same output as the original program, as shown in Listing 2.

   Saving XML attributes

The new properties also have a feature that stores attributes in XML-formatted files. Although the store () method still creates a file similar to that shown in Listing 1, you can now create the file shown in Listing 5 with the new Storetoxml () method. Just pass a outputstream and a String for annotation. Listing 7 shows the new Storetoxml () method.

Listing 7. To store Properties as XML files

Import java.util.*;
Import java.io.*;

public class StoreXML {
public static void Main (String args[]) throws Exception {
Properties prop = new properties ();
Prop.setproperty ("one-two", "Buckle My Shoe");
Prop.setproperty ("Three-four", "Shut the Door");
Prop.setproperty ("Five-six", "Pick Up Sticks");
Prop.setproperty ("Seven-eight", "lay them straight");
Prop.setproperty ("Nine-ten", "a big, Fat hen");
FileOutputStream fos = new FileOutputStream ("Rhyme.xml");
Prop.storetoxml (FOS, "rhyme");
Fos.close ();
}
}


Running the program in Listing 7 produces output as shown in Listing 8.

Listing 8. Stored XML files

! DOCTYPE Properties SYSTEM "HTTP://JAVA.SUN.COM/DTD/PROPERTIES.DTD" >

Rhyme
lay them straight
pick Up Sticks
a big, fat hen
shut the door
buckle my shoe


   Concluding remarks

Using an XML file or using an old-fashioned a=b type of file depends entirely on you. Vintage files are certainly lightweight from a memory perspective. However, because of the widespread use of XML, it is expected that the XML format will be popular because it is already widely used, except that the Properties object is not used. The choice is entirely on you. Analyze the source code for the private Xmlutils class of the package for more information about the XML parsing you are using.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.