Java.util.Properties Loading properties from XML

Source: Internet
Author: User
Tags cdata

1. Methods of basic Load properties

Sample Properties File

Sample.properties

Foo=bar

Fu=baz

Sample for loading properties 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"));

}

}

The output results are as follows:

--Listing properties--

Fu=baz

Foo=bar

The Foo Property:bar

2. Loading properties from XML

here is the properties DTD list:

<?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 >

Sample XML Properties 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

from an XML file Load properties of sample program

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"));

}

}

The results of the output are the same

You 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 values in the XML file

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"));

}

}

The example above loads the attributes in Sample.xml, updates the value of the Foo property, adds the New-name attribute, invokes the Storetoxml () method to save it to the original file, and changes the comment to store Sample

sample.xml after the execution of the program the contents 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.