I want to implement an application where I have various objects that can be interpreted as XML strings. At first I thought of creating an interface that made each object implement two methods:
public abstract Element toXML(); public abstract void fromXML(Element element);
The first converts information about the object into a DOM element, and the second transfers information to the object from the DOM element. I ended up defining a static string in each subclass containing the TAG of the element, so I decided to turn the interface into an abstract class and provide it with 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 elements. The fault of this project, which I cannot overcome, is that the TAG attribute cannot be static as I want, mainly because I do not want to instantiate and object of each subclass in order to know TAG it uses. Java does not allow overriding static attributes or methods, what is the right way to overcome this?
source share