Monday 6 March 2017

Java Tutorial: Properties class in java[How to load key-value pairs from xml]


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=vKZFsTAoGw8&list=UUhwKlOVR041tngjerWxVccw

properties.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="Width">15</entry>
<entry key="Height">200</entry>
</properties>

PropertiesDemo.java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

/*
 * public void loadFromXML(InputStream in) throws
 *                      IOException, InvalidPropertiesFormatException
 * 
 * Parameters: 
 * -----------
 * 
 * in - the input stream from which to read the XML
 * document.
 */
class PropertiesDemo
{

    public static void main(String[] args) throws IOException
    {
        Properties p = new Properties();

        try (FileInputStream fis = new FileInputStream("properties.xml"))
        {
            /*
             * Loads all of the properties represented by
             * the XML document on the specified input
             * stream into this properties table.
             */
            p.loadFromXML(fis);

            Enumeration<?> enumeration = p.propertyNames();

            while (enumeration.hasMoreElements())
            {
                String key = (String) enumeration.nextElement();
                String value = p.getProperty(key);

                System.out.println(key + " = " + value);
            }

        }

    }
}
Output
Width = 15
Height = 200

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/PropertiesDemo_loadFromXML_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/PropertiesDemo_loadFromXML_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/2010ece32ddd64329464f57cc3a56f059cb8f810/BasicJava/PropertiesDemo_loadFromXML_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment