Tiger Series One: Loading properties from XML

Source: Internet
Author: User
Tags cdata xml attribute
The Java.util.Properties class is updated in the xml| load JDK1.5 (code-named Tiger), providing a simple way to read and write Key-value attributes from an XML file: LoadFromXML () and Storetoxml ()

1, basic load properties of the method

L Sample Properties File: Sample.properties

Foo=bar
Fu=baz
L Loading properties of sample program

Import Java.io.FileInputStream;
Import java.util.Properties;

public class Loadsampleproperties {

public static void Main (string[] args) throws Exception {
Properties prop = new properties ();
FileInputStream fis = new FileInputStream ("Props/sample.properties");
Prop.load (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" + Prop.getproperty ("foo"));
}
}
L OUTPUT The following results:

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

The Foo Property:bar


2. Loading properties from XML

L The following is a list of properties DTDs:

<?xml version= "1.0" encoding= "UTF-8"?>
<!--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 >
l Sample XML attribute file: Sample.xml (conforming to the properties DTD above)

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" >
<properties>
<comment>Hello</comment>
<entry key= "foo" >bar</entry>
<entry key= "Fu" >baz</entry>
</properties>
L <entry> Tags Specify a property, the property name is specified by the key property, and the contents of the <entry> tag specify the property value

L <comment> tags can be used to specify annotations

L A sample program that loads attributes from an XML file

Import Java.io.FileInputStream;
Import java.util.Properties;

public class Loadsamplexml {

public static void Main (string[] args) throws Exception {
Properties prop = new properties ();
FileInputStream fis = new FileInputStream ("Props/sample.xml");
Prop.loadfromxml (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" + Prop.getproperty ("foo"));
}
}
L output is the same as the result

L can see that the method is simple: Use an XML file to save the property, and use the LoadFromXML () method instead of the original load () method to load the attribute in the XML file



3, update the attribute value in the XML file

L Sample Program

Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.util.Properties;

public class Updatesamplexml {

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

Prop.setproperty ("foo", "Hello world!");
Prop.setproperty ("New-name", "New-value");
FileOutputStream fos = new FileOutputStream ("Props/sample.xml");
Prop.storetoxml (FOS, "Store Sample");
Fos.close ();

FIS = new FileInputStream ("Props/sample.xml");
Prop.loadfromxml (FIS);
Prop.list (System.out);
System.out.println ("\nthe foo property:" + Prop.getproperty ("foo"));
}
}
L The example above loads the attributes in Sample.xml, updates the value of the Foo property, adds the New-name property, invokes the Storetoxml () method to save it to the original file, and changes the comment to store Sample

The contents of the Sample.xml following the implementation of the program are as follows:

<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Properties SYSTEM "Http://java.sun.com/dtd/properties.dtd" >
<properties>
<comment>store sample</comment>
<entry key= "New-name" >new-value</entry>
<entry key= "Fu" >baz</entry>
<entry key= "foo" >hello world!</entry>
</properties>




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.