Java – Moving Static Variables to XML

javastaticxml

I've been working on a small Java project by myself for a department that does not do a lot of software development but mostly database stuff.

I showed my boss the code I've been writing and he saw that in some classes there were some static variables. He then told me to put the static variables in XML instead to make it "easier."

He left and I did not get to ask him how it makes them manageable. He's going to be gone for a couple of weeks.

I know this might be a pretty vague question but what could possibly be the reasons why he would tell me to put static variables into XML? And how do I go about doing them?

First, I had something like this:

private final static String[] ACCESS_EXTENSIONS = {"accdb", "mdb"};

After he told me about putting static variables in an XML file, I wrote an XML code:

<input_filetypes>

    <filetype id = "access">
        <title>Access Database</title>
        <extension>accdb</extension>
        <extension>mdb</extension>
    </filetype>

    <filetype id = ....>
        ....
    </filetype>

    ......

</input_filetypes>

But then this resulted to me creating a static String variable that contains the location of my XML file:

private final static String xmlFile = "location_of_my_xml_file";

I also learned about how to use XPath to access data in my XML, but I ended up writing another static code which is:

private final static String GET_ACCESS_EXTNS_EXP =
    "input_filetypes/filetype[@id='access']/extension";

Now I think I made things worse. I know I should wait for him to get back to clarify, but that is a long time from now, and I think it's best if I start reading up while waiting. I just need ideas on where to start.

Best Answer

Storing the configuration of the program externally allows you to avoid needing to recompile the program when something changes. Switch from databaseA to databaseB? Modify the property file rather than modify the hard coded values and recompile from source.

What's more, Java offers a class as part of its standard runtime environment that facilitates this - the Properties class. This class can either take a .properties file or an .xml file that matches the dtd specified at http://java.sun.com/dtd/properties.dtd (it really is a rather simple one - its the same as the one in the javadoc).

With the Properties class, you load it in, and then you can easily call getProperty(String key) or getProperty(String key, String defaultValue) and get your values out of it. This is much easier than loading values from a custom xml file and all the xml or xpath processing that you would have to do with it. Furthermore, its a known thing and easier to get right.

That's the property file.

access.extensions = accdb,mdb # where you might find an access database

And then you've got the corresponding bit of Java...

String[] extensions = prop.getproperty("access.extensions", "accdb").split(",");

for reading it out. This makes the code a bit more self documenting, a safeguard against not having the file with a default value.

If you really wanted to use xml instead of a properties file, it would look like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
  <comment>Access Database Extensions</comment>
  <entry key="access.extensions">accdb,mdb</entry>
</properties>

But again, the key point is to move the values out of the code and into external files that are easier to work with. And Java already has the tools for reading those back into the system.