Read/Write System Properties From/Into XML Using Groovy

[ This Blog Was Moved To : http://www.diknows.com ]


Inspired by the following post ( http://www.ibm.com/developerworks/library/j-tiger02254.html ), I created the following code snippets to Read / Write System properties from/into XML files.

Writing system properties into XML file (output.xml):

def properties = System.properties
def fos = new FileOutputStream("output.xml")
properties.storeToXML(fos,"")

Reading system properties from XML file (input.xml):

def prop = new Properties()
def fis = new FileInputStream("input.xml")
prop.loadFromXML(fis)

And, ofcourse this line can be used to list the properties in any properties object

prop.list(System.out)

Here is the resultant XML File:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
 <comment/>
 <entry key="java.runtime.name">Java(TM) SE Runtime Environment</entry>
 <entry key="sun.boot.library.path">/opt/jdk1.6.0_17/jre/lib/amd64</entry>
 <entry key="java.vm.version">14.3-b01</entry>
 <entry key="java.vm.vendor">Sun Microsystems Inc.</entry>
 <entry key="java.vendor.url">http://java.sun.com/</entry>
 <entry key="path.separator">:</entry>
 <entry key="java.vm.name">Java HotSpot(TM) 64-Bit Server VM</entry>
 <entry key="file.encoding.pkg">sun.io</entry>
 <entry key="sun.java.launcher">SUN_STANDARD</entry>
 <entry key="user.country">US</entry>
 <entry key="sun.os.patch.level">unknown</entry>
 <entry key="program.name">groovyConsole</entry>
 <entry key="java.vm.specification.name">Java Virtual Machine Specification</entry>
 <entry key="user.dir">/home/dino/Documents</entry>
 <entry key="java.runtime.version">1.6.0_17-b04</entry>
 <entry key="java.awt.graphicsenv">sun.awt.X11GraphicsEnvironment</entry>
 <entry key="java.endorsed.dirs">/opt/jdk1.6.0_17/jre/lib/endorsed</entry>
 <entry key="os.arch">amd64</entry>
 <entry key="java.io.tmpdir">/tmp</entry>
 <entry key="line.separator">
 </entry>
 <entry key="java.vm.specification.vendor">Sun Microsystems Inc.</entry>
 <entry key="os.name">Linux</entry>
 <entry key="tools.jar">/opt/jdk1.6.0_17//lib/tools.jar</entry>
 <entry key="sun.jnu.encoding">UTF-8</entry>
 <entry key="script.name">/home/dino/installations/groovy/groovy-1.6.5/bin/groovyConsole</entry>
 <entry key="java.library.path">/opt/jdk1.6.0_17/jre/lib/amd64/server:/opt/jdk1.6.0_17/jre/lib/amd64:/opt/jdk1.6.0_17/jre/../lib/amd64:/usr/lib64/mpi/gcc/openmpi/lib64:/usr/java/packages/lib/amd64:/lib:/usr/lib</entry>
 <entry key="java.specification.name">Java Platform API Specification</entry>
 <entry key="java.class.version">50.0</entry>
 <entry key="sun.management.compiler">HotSpot 64-Bit Server Compiler</entry>
 <entry key="os.version">2.6.31.5-0.1-desktop</entry>
 <entry key="user.home">/home/dino</entry>
 <entry key="user.timezone">Africa/Cairo</entry>
 <entry key="java.awt.printerjob">sun.print.PSPrinterJob</entry>
 <entry key="file.encoding">UTF-8</entry>
 <entry key="java.specification.version">1.6</entry>
 <entry key="java.class.path">/home/dino/installations/groovy/groovy-1.6.5/lib/groovy-1.6.5.jar</entry>
 <entry key="user.name">dino</entry>
 <entry key="java.vm.specification.version">1.0</entry>
 <entry key="java.home">/opt/jdk1.6.0_17/jre</entry>
 <entry key="sun.arch.data.model">64</entry>
 <entry key="user.language">en</entry>
 <entry key="java.specification.vendor">Sun Microsystems Inc.</entry>
 <entry key="java.vm.info">mixed mode</entry>
 <entry key="java.version">1.6.0_17</entry>
 <entry key="java.ext.dirs">/opt/jdk1.6.0_17/jre/lib/ext:/usr/java/packages/lib/ext</entry>
 <entry key="sun.boot.class.path">/opt/jdk1.6.0_17/jre/lib/resources.jar:/opt/jdk1.6.0_17/jre/lib/rt.jar:/opt/jdk1.6.0_17/jre/lib/sunrsasign.jar:/opt/jdk1.6.0_17/jre/lib/jsse.jar:/opt/jdk1.6.0_17/jre/lib/jce.jar:/opt/jdk1.6.0_17/jre/lib/charsets.jar:/opt/jdk1.6.0_17/jre/classes</entry>
 <entry key="java.vendor">Sun Microsystems Inc.</entry>
 <entry key="file.separator">/</entry>
 <entry key="java.vendor.url.bug">http://java.sun.com/cgi-bin/bugreport.cgi</entry>
 <entry key="sun.io.unicode.encoding">UnicodeLittle</entry>
 <entry key="sun.cpu.endian">little</entry>
 <entry key="groovy.sanitized.stacktraces">org.codehaus.groovy.runtime.
 org.codehaus.groovy.
 groovy.lang.
 gjdk.groovy.lang.
 sun.
 java.lang.reflect.
 java.lang.Thread
 groovy.ui.Console</entry>
 <entry key="groovy.full.stacktrace">false</entry>
 <entry key="groovy.starter.conf">/home/dino/installations/groovy/groovy-1.6.5/conf/groovy-starter.conf</entry>
 <entry key="groovy.home">/home/dino/installations/groovy/groovy-1.6.5</entry>
 <entry key="sun.cpu.isalist"/>
</properties>

And this is it for now.