Tam Tiger: loading attributes from XML
Termination key - value confusion
John Zukowski
(Jaz@zukowski.net) President, JZ Ventures, Inc. March 2004
J2SE 1.5 previous version requirements use the XML parser directly to load the configuration file and store the settings. Although this is not a difficult thing, and the parser is the standard part of the platform, and additional work is always a bit annoying. The recently updated Java.util.properties class now provides a more easier method for loading and storage settings: LoadFromxml (InputStream IS, String Comment) method.
Properties Basic Knowledge If you are not familiar with the Java.util.Properties class, then now tell you that it is used to store keys in a file, where the keys and values are separated by equal sign, as shown in Listing 1.
Listing 1. A group of properties examples
FOO = BAR
Fu = baz
After loading the list 1 into the Properties object, you can find two keys (foo and fu) and two values (Foo Bar and FU BAZ). This class supports / u is embedded in a Unicode string, but it is important that each content is treated as a string.
Now there is a J2SE 1.5 Beta 1, the first phase of this column was published in January 2004, Sun released the J2SE 1.5 Beta 1 version. Be sure to replace the ALPHA version mentioned in the first phase with the latest Beta version.
Listing 2 shows how to load the properties file and list its current set of keys and values. Simply deliver the files of this file to the LOAD () method, you will add each key-value to the Properties instance. Then list all attributes with list () to obtain a separate property with getProperty ().
Listing 2. Loading 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 (FIA);
Prop.List (System.out);
System.out.println ("/ NTHE Foo Property:"
Prop.GetProperty ("foo"));
}
}
Run the LoadSample program generates the output shown in Listing 3. Note that the output of the List () method is not the same as the order in which the value is in the order in the input file. The Properties class stores a set of key-value pairs in a hash table (HashTable, in fact is a HashTable subclass), so it cannot guarantee the order.
Listing 3. LOADSAMPLE output
- Listing Properties -
Fu = baz
FOO = BAR
The Foo Property: BAR
XML attribute file There is no new content here. The Properties class is always working like this. However, the new place is to load a group of properties from an XML file. Its DTD is shown in Listing 4.
Listing 4. Properties DTD
If you don't want to read XML DTD, you can tell you that it is actually in the periphery.
The package is packaged in the label.
Label, behind is any number of labels. For each one
Tags, there is a key attribute, the content entered is its value. Listing 5 shows what the XML version of the attribute file in Listing 1 looks like.
Listing 5. Attribute file of XML version
Hi
Bar
BAZ
If the list 6 shows that the Properties file that reads the XML version is not different from the file read old format.
Listing 6. Read XML Properties files
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"));
}
}
Description of Resource Binding Although the Java.util.Properties class now In addition to support key-value pairs, the property file is also supported as an XML file. Unfortunately, there is no built-in option to process resourcebundle as an XML file. Yes, PropertyResourceBundle does not load binding without using the Properties object, but the use of the load method is hardcoded to the class without using a new LoadFromXML () method.
The program in Run Listing 6 produces the same output as the original program, as shown in Listing 2.
Saving XML Property New Properties has a function to store attributes into an XML format file. Although the Store () method still creates a file similar to Listing 1, you can now create the file shown in Listing 5 with a new StoreToxML () method. Just deliver an OutputStream and a string for comments. Listing 7 shows the new StoreToxml () method.
Listing 7. Store Properties as an XML file
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 UPS");
Prop.SetProperty ("Seven-Eight", "Lay The Straight");
Prop.SetProperty ("Nine-Ten", "A Big, Fat Hen");
FileOutputStream Fos =
New FileOutputStream ("rhyme.xml");
Prop.StoreToxml (FOS, "Rhyme");
Fos.close ();
}
}
The output generated by the program in Operation Listing 7 is shown in Listing 8.
Listing 8. Store XML file
Rhyme
Lay Them Straight
PICK UPSICKS
A Big, Fat Hen
Shut the door
Buckle My Shoe
End language uses XML files or uses old-fashioned A = B type files depending on your own. Vintage files are definitely lightweight from the perspective of memory. However, due to the common use of XML, people will expect XML formats to be popular because it has been widely used, but it is not used to use the Properties object. Choose to be completely at you. Analyze the source code for the package Private XMLUTILS class to obtain more information about the XML parses used.