QUOTE(reCoded @ 3 Jun, 2008 - 10:49 AM)

Hey all,
I was wondering if anyone knows where I can find out more about creating a Java config file using XML. I searched around and havent found anything decent.
Thanks,
reCoded
Why do you want to create an XML config file ?
You want to save some values that you will read back later ?
The XML will be read with another language ?
If it is NOT the case a lot lot easier to use an object writer and reader.
Just telling you to consider this alternative... if you still want to go XML will go XML later but I told you a lot more complicated.
Let say that you can save all your configuration in a class Config that would look like this
CODE
import java.awt.Color;
import java.io.*;
/** This class contains config info */
public class Config implements Serializable {
public int nbRow, nbColumn;
public Color color;
public String text;
// constructor
public Config(int n, int m, Color col, String str) {
nbRow = n;
nbColumn = m;
color = col;
text = str;
}
}
OK you have to:
import java.io.*;
and declare that your class "implements Serializable"
now this is the driver for it
It will create a file, instantiate an Object Config
write it to file
read it back
CODE
import java.awt.Color;
import java.io.*;
public class Main {
public static void main(String[] arg) {
try {
FileOutputStream fos = new FileOutputStream("config.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
// create a config object
Config x = new Config(10, 20, Color.WHITE, "This the text");
// write it
oos.writeObject(x);
oos.close();
}
catch(IOException e) {
System.out.println("Problem creating file config.txt: " + e);
return;
}
// read back the object
Config readBack;
try {
FileInputStream fis = new FileInputStream("config.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
readBack = (Config) ois.readObject();
ois.close();
}
catch(Exception e) {
System.out.println("Problem reading back config.txt: " + e);
return;
}
System.out.println("Read back: " + readBack.nbColumn + " " + readBack.nbRow);
}
}
Now the version with XML. You'll have to specify the name of every instance variable that you want to keep
CODE
import java.awt.Color;
import java.beans.*;
import java.io.*;
public class Main {
public static void main(String[] arg) {
try {
FileOutputStream fos = new FileOutputStream("config.xml");
XMLEncoder encoder = new XMLEncoder(fos);
encoder.setPersistenceDelegate(Config.class,
new DefaultPersistenceDelegate(new String[]{"nbRow", "nbColumn", "color", "text"}));
// create a config object
Config x = new Config(10, 20, Color.WHITE, "This the text");
// write it
encoder.writeObject(x);
encoder.close();
}
catch(IOException e) {
System.out.println("Problem creating file config.xml: " + e);
return;
}
// ok read back
Config readBack;
try {
FileInputStream fis = new FileInputStream("config.xml");
XMLDecoder decoder = new XMLDecoder(fis);
readBack = (Config) decoder.readObject();
decoder.close();
}
catch(IOException e) {
System.out.println("Problem reading back file config.xml: " + e);
return;
}
System.out.println("ReadBack: " + readBack.nbColumn + " " + readBack.nbRow + " " + readBack.color + " " + readBack.text);
}
}
Now if you want to save Swing elements (JButtons, JTable, ...) you will have to use the XML version if you want your application to be able to read back the swing elements in a newer version of the JRE
If you use the XML version, it would be a good idea to centralize in the class Congig the names of the instance variables to save. So if ever you change your class (adding a variable) you will have to change just that method. So you can add to the Config class
CODE
// returns the name of the variables to save
static String[] getDelegate() {
return new String[]{"nbRow", "nbColumn", "color", "text"};
}
and now in the main method you can do:
CODE
encoder.setPersistenceDelegate(Config.class,
new DefaultPersistenceDelegate(Config.getDelegate()));
This post has been edited by pbl: 3 Jun, 2008 - 12:11 PM