I want to implement an aplication where I have various Objects that can be interpreted as XML Strings. First I thought of making an interface which made each object implement two methods:
public abstract Element toXML();
public abstract void fromXML(Element element);
The first one turns the object information into the an DOM Element an the secondone loads the information to the object from a DOM element. I ended up defining an static String in each subclass holding the TAG of the element, so I decided to turn the interface into an abstract class and give it more functionality:
public abstract class XmlElement implements Serializable {
protected static Document elementGenerator;
public String TAG = "undefined";
static {
try {
elementGenerator = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (ParserConfigurationException e) {
StateController.getInstance().addLog(
new Log(Log.Type.ERROR, "Couldn't load XML parser: " + e));
System.exit(1);
}
}
public abstract Element toXML();
public abstract void fromXML(Element element);
}
The element generator is used in the toXML method to generate the Elements. The fault of this design that I'm unable to overcome is that the TAG attribute can't be made static as I wish it to be, mostly because I don't want to instantiate and object of each subclass just to know the TAG it uses. Java doesn't allow to override Static attributes or methods, which is the correct way to overcome this?