Properties void storeToXML(OutputStream os,String comment,String encoding) method Example Program


Emits an XML document representing all of the properties contained in this table, using the specified encoding.

Program

package com.candidjava;

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

/* 
 * @author : Mohan 
 * @description : Emits an XML document representing all of the properties contained in this table, using the specified encoding.
 */
public class PropertiesStoreToXmlEncode {

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

		// add some properties
		prop.put("height", "200");
		prop.put("level", "15");
		prop.put("Event", "33");

		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 and a different encoding
			prop.storeToXML(fos, "Properties Example", "ISO 8859");

			// print the xml. Notice that ISO 8859 isn't supported
			while (fis.available() > 0) {
				System.out.print("" + (char) fis.read());
			}

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

	}
}

Output

Warning:  The encoding 'ISO 8859' is not supported by the Java runtime.
Warning: encoding "ISO 8859" not supported, using UTF-8
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="height">15</entry>
<entry key="level">200</entry>
<entry key="event">33</entry>
</properties>

Explanation

public void storeToXML(OutputStream os, String comment, String encoding)throws IOException
Emits an XML document representing all of the properties contained in this table, using the specified encoding.
The XML document will have the following DOCTYPE declaration:

 <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
 
If the specified comment is null then no comment will be stored in the document.

The specified stream remains open after this method returns.

Parameters:
os - the output stream on which to emit the XML document.
comment - a description of the property list, or null if no comment is desired.
encoding - the name of a supported character encoding
Throws:
IOException - if writing to the specified output stream results in an IOException.
NullPointerException - if os is null, or if encoding is null.
ClassCastException - if this Properties object contains any keys or values that are not Strings.
Since:
1.5
See Also:
loadFromXML(InputStream)


Related Post

Comments


©candidjava.com