I'm using the JDOM library in a solution.
I have created the following class because I want to have added capabilities (mostly get
methods to parse specific types of data from XML elements).
public class MyElement extends Element {
// methods such as...
public Boolean getBoolean(){
}
}
Of course, the elements with data are not the root element, so what I want do is this:
// MyElement variable "data" has been assigned before
Boolean isTest = data.getChild("isTest").getBoolean();
The problem is that getChild
returns an Element
object (as implemented by the superclass), which in turn does not know the subclass method.
From what I've read in other questions, downcasting wouldn't work?
I have thought of overriding the getChild
method but there a downcast would be needed too, right?
Is it possible to use or override the superclass methods so that returned Element
objects can be seen as MyElement
objects?
I have found this question but that assumes you can alter the superclass's methods too.
I would probably make a static class to hold "extension methods" for Element. This is similar to Integer.parseInt(String s);
Boolean isTest = MyElement.getBoolean(data.getChild("isTest"));
An implementation would be
public static class MyElement {
public static boolean getBoolean(Element e) {
// Do your thing.
return e.getValue() == "true" || e.getValue() == "1";
}
}
You could check the instance of Element to see if it is MyElement and then cast it. But this cast will only succeed if the Element is actually of that instance. Meaning that you added constructed it and added it to your Document.
Hope this helps.