xml property file...for say db connections?

T

timasmith

I have code that connects to MS Access *and* code which connects to an
Oracle database.

Does anyone have a code snippet which reads an XML file to extract
properties I can use to dynamically determine my configuration.

This is less of a db problem - more of extracting properties from an
XML config file problem. Ideally my file is something like this

<db>
<environment name="default">
<driver>MSAccess</driver>
<datasource>C:\temp\my.mdb</datasource>
</environment>
<environment name="production">
<driver>Oracle</driver>
<datasource>tnsnamesname</datasource>
<node>mynode</node>
<port>1521</port>
<instance>prod1</instance>
</environment>
</db>

Is there any existing java framework class that simplifies this task?

thanks

Tim
 
B

Bjorn Abelli

I have code that connects to MS Access *and* code
which connects to an Oracle database.

Does anyone have a code snippet which reads an
XML file to extract properties I can use to
dynamically determine my configuration.

I guess you have these properties in a special object?

Then you can simply use java.beans.XMLCoder and XMLDecoder to serialize that
object in XML.

I found a hack I made several years ago for an app, where I easily could
shift between different connections, not only Oracle and MS Access... ;-)

It's not pretty, but I don't have the time to fix it.

Hopefully you can look at it and see if it's of any use to you.

======================================

private void saveConnections ()
{
try
{
XMLEncoder e =
new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("Connections.xml")));

Vector cbm = new Vector(connections.values());

int antal = cbm.size();

for (int i = 0; i < antal; i++)
{
ConnectionProperty c =
(ConnectionProperty)
cbm.get(i);
e.writeObject(c);
}

e.close();
}
catch (FileNotFoundException fex)
{
fex.printStackTrace();
}
}

private void loadConnections ()
{
java.beans.XMLDecoder d = null;

try
{
d = new XMLDecoder(
new BufferedInputStream(
new FileInputStream("Connections.xml")));

while (true)
{
ConnectionProperty ci = (ConnectionProperty) d.readObject();
connections.put(ci.getLabel(), ci);
}
}
catch (FileNotFoundException fex)
{
fex.printStackTrace();
}
catch (ArrayIndexOutOfBoundsException aix)
{
d.close();
}
}

======================================

And the class I used for storing the
Connection properties...

======================================

import java.io.*;

public class ConnectionProperty implements Serializable
{
private String label;
private String driver;
private String jar;
private String connectionString;

public ConnectionProperty (String l, String d, String c, String j)
{
label = l;
driver = d;
connectionString = c;
jar = j;
}

public ConnectionProperty ()
{
// For the "Serializability"
}

public String toString()
{
return label;
}

public String getDriver()
{
return driver;
}

public void setDriver(String d)
{
driver = d;
}

public String getJar()
{
return jar;
}

public void setJar(String d)
{
jar = d;
}

public String getLabel()
{
return label;
}

public void setLabel(String s)
{
label = s;
}

public String getConnectionString()
{
return connectionString;
}

public void setConnectionString(String c)
{
connectionString = c;
}
}


======================================

// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
A

Arvind

I have code that connects to MS Access *and* code which connects to an
Oracle database.

Does anyone have a code snippet which reads an XML file to extract
properties I can use to dynamically determine my configuration.

This is less of a db problem - more of extracting properties from an
XML config file problem. Ideally my file is something like this

<db>
<environment name="default">
<driver>MSAccess</driver>
<datasource>C:\temp\my.mdb</datasource>
</environment>
<environment name="production">
<driver>Oracle</driver>
<datasource>tnsnamesname</datasource>
<node>mynode</node>
<port>1521</port>
<instance>prod1</instance>
</environment>
</db>

Is there any existing java framework class that simplifies this task?
There are many frameworks that let you map XML - Java and gets you
going...like Digester, Castor XML etc....

You can even consider storing such environment specific thingies in a
database too or different property files !

No matter what route you choose, make sure, the load of such
information happens once and remains a singleton since that time
 
R

RC

Does anyone have a code snippet which reads an XML file to extract
properties I can use to dynamically determine my configuration.

Do more research on DOM and/or SAX XML parsers.
They are existed for many years, they are standard.

Go to the link below click
Xalan and/or Xerces
I think O'Reilly book(s) Java XML will teach you how to use these
DOM/SAX XML parsers


http://xml.apache.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top