Welcome to Dream.In.Code
Become a Java Expert!

Join 150,027 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,574 people online right now. Registration is fast and FREE... Join Now!




XML java config file

2 Pages V  1 2 >  
Reply to this topicStart new topic

XML java config file

reCoded
3 Jun, 2008 - 09:49 AM
Post #1

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

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

This post has been edited by reCoded: 3 Jun, 2008 - 09:50 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: XML Java Config File
3 Jun, 2008 - 11:23 AM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
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
User is online!Profile CardPM
+Quote Post

reCoded
RE: XML Java Config File
3 Jun, 2008 - 12:40 PM
Post #3

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

Sorry I should have specified why I want to create a Config file. First being that on my application that I am developing there is an image. I want to be able to change that image in the config file. Second, I was asked to. If doing this in XML is more than its worth I would appreciate some reasons as to why that is.

But if it would'nt be too much trouble, I would like to see how it could be done in XML. I know C#.NET has Config files that are in XML. I was just curious as to if it can be done in Java since I was asked to do such and where I can find some examples or something along those lines to get an idea how it is done.

Thanks,
reCoded.
User is offlineProfile CardPM
+Quote Post

pbl
RE: XML Java Config File
3 Jun, 2008 - 01:01 PM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
If your image object is in Config class and if you specify to save an instance of Config it will go into your .xml file

Of it is an ImageIcon just save it:

encoder.writeObject(image);

and restore

image = (ImageIcon) decoder.readObject()
but it will only save the name of the .gif or .jpg from where the image comes

This post has been edited by pbl: 3 Jun, 2008 - 02:08 PM
User is online!Profile CardPM
+Quote Post

reCoded
RE: XML Java Config File
3 Jun, 2008 - 05:08 PM
Post #5

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

QUOTE(pbl @ 3 Jun, 2008 - 02:01 PM) *

If your image object is in Config class and if you specify to save an instance of Config it will go into your .xml file

Of it is an ImageIcon just save it:

encoder.writeObject(image);

and restore

image = (ImageIcon) decoder.readObject()
but it will only save the name of the .gif or .jpg from where the image comes



Is there any way I can make the XML file a config file for my application..So all I have to do is change it in the XML file and it will update my application?
User is offlineProfile CardPM
+Quote Post

pbl
RE: XML Java Config File
3 Jun, 2008 - 06:30 PM
Post #6

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,586



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(reCoded @ 3 Jun, 2008 - 06:08 PM) *


Is there any way I can make the XML file a config file for my application..So all I have to do is change it in the XML file and it will update my application?


We are back to the config file ?

If you take the first example it would generate a file like this

CODE

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.5.0_11" class="java.beans.XMLDecoder">
<object class="Config">
  <int>10</int>
  <int>20</int>
  <object class="java.awt.Color">
   <int>255</int>
   <int>255</int>
   <int>255</int>
   <int>255</int>
  </object>
  <string>This the text</string>
</object>
</java>


You use an editor and change the first 2 int which are nbRow, nbColumn, or the color or the String text

Is that what you mean ?

User is online!Profile CardPM
+Quote Post

1lacca
RE: XML Java Config File
4 Jun, 2008 - 02:17 AM
Post #7

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
An XML config file is a good idea, because it can be ported from one platform to another.
The object writer method has a serious drawback: if you modify your config object, the earlier serialized objects will throw a nice exception when you try to read them back, however keeping a config file during a version update is usually a nice thing. (It is possible to write a custom serializer and deserializer, but than I think since than it is not the easier anymore)
I would recommend taking a look at the java.util.Properties class, or using any traditional XML handling method.
User is offlineProfile CardPM
+Quote Post

reCoded
RE: XML Java Config File
4 Jun, 2008 - 04:35 AM
Post #8

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

Hmm, maybe I'm a little confused and I apologize. What I am looking to do in this config file is allow for an image to be changed, address to where the application is connecting to, allowing for more rows and columns to be added as well as new column header text. Things of that nature. I apologize for my lack of knowledge as to how this is done in java.

Thanks again,
reCoded
User is offlineProfile CardPM
+Quote Post

1lacca
RE: XML Java Config File
4 Jun, 2008 - 05:01 AM
Post #9

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
I'd suggest with going with an XML parser you find the easiest to use.
You can find a list and some examples here

Well, that link is quite outdated, maybe this is better.
User is offlineProfile CardPM
+Quote Post

reCoded
RE: XML Java Config File
4 Jun, 2008 - 05:17 AM
Post #10

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

Is this a configuration file for a XML app or for a Java app... Phew... I am confused.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: XML Java Config File
4 Jun, 2008 - 05:26 AM
Post #11

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
XML is just a format, you can store whatever data you want in it.
It is just a way of dealing with XML files, so you don't have to write a parser yourself.
In your case you want to store (whatever)application configuration data in XML, so you'll create an XML file that'll store your settings.
User is offlineProfile CardPM
+Quote Post

reCoded
RE: XML Java Config File
4 Jun, 2008 - 05:30 AM
Post #12

D.I.C Head
**

Joined: 25 Feb, 2008
Posts: 166

Ok I think I understand how this XML thing works. Now, how do I go about creating a XML file in Java?

This post has been edited by reCoded: 4 Jun, 2008 - 05:58 AM
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:30PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month