Java.util.Properties.loadFromXML() Method

Advertisements


Description

The java.util.Properties.loadFromXML(InputStream in) method loads all of the properties represented by the XML document on the specified input stream into this properties table.

Declaration

Following is the declaration for java.util.Properties.loadFromXML() method

public void loadFromXML(InputStream in)

Parameters

  • in -- the input stream from which to read the XML document.

Return Value

This method does not return a value.

Exception

  • IOException -- if reading from the specified input stream results in an IOException.

  • InvalidPropertiesFormatException -- Data on input stream does not constitute a valid XML document with the mandated document type.

  • NullPointerException -- if in is null.

Example

The following example shows the usage of java.util.Properties.loadFromXML() method.

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

public class PropertiesDemo {

   public static void main(String[] args) {
      Properties prop = new Properties();

      // add some properties
      prop.put("Height", "200");
      prop.put("Width", "15");

      try {

         // create a output and input as a xml file
         FileOutputStream fos = new FileOutputStream("properties.xml");
         FileInputStream fis = new FileInputStream("properties.xml");

         // store the properties in the specific xml
         prop.storeToXML(fos, null);

         // load from the xml that we saved earlier
         prop.loadFromXML(fis);

         // print the properties list
         prop.list(System.out);

      } catch (IOException ex) {
         ex.printStackTrace();
      }

   }
}

Let us compile and run the above program, this will produce the following result:

-- listing properties --
Width=15
Height=200



Advertisements
Advertisements