I have a couple of domain classes which all inherit a BaseDomain class.
In this BaseDomain class I have a field that is public final String name;
The value of name is set in the constructor.
public class BaseDomain {
public final String name;
public BaseDomain() {
this.name = this.getClass().getCanonicalName();
}
}
This BaseDomain is extended by a few classes
public class Trip extends BaseDomain {
private int id;
public Trip(int id){
this.id = id;
}
}
So far so good.
I want to get the value of the field "name" in an object instance of Trip of a with the help of JXPath but can't. I can access the "id" field but not the "name" field.
JXPathContext jxPathContext = JXPathContext.newContext(trip);
jxPathContext.setLenient(true);
int id = (int)jxPathContext.getValue("/@id"); // This works.
String name = (String)jxPathContext.getValue("/@name"); // This does not work.
Is it possible to get the value of "name" with this setup and JXPath?
The code might have some syntax errors and/or other errors. I hope you all get the idea and understand my question.
Any help or pointer are welcome.
First of: I want to thank Kelly S. French for his quick replay. It made me realize that I have to explain some more.
I want to use jxpath because I will eventually do a deeper search. For example: The Trip might hold a list of Locations which also extends the BaseDomain. Each Location can hold a list of PointOfInterest that extends BaseDomain.
Via reflection in other part of the code I want to be able to get a list of BaseDomain based on their type (class.getCanonicalName())
The object-tree is not based on xml, it is pure POJO.
As far as I have figured out, there is no way of writing a jxpath-query for finding a list of objects based on their type, class name and so on.
Is this correct? Does some one know of a way to do that?
The easiest way out, even if it's ugly, is to have a field in the super class that holds the class-name. That is why I have done this ugly solution.
Eventually I want to create a jxpath-query that based on the trip returns an iterator of which ever object that is an instance of BaseDomain at any depth and not depending on which branch in the object tree the node is located, as long as I can get the class-name of the object I'm looking for.
Does any one know if it is possible to achive this with a jxpath-query?
Code example, links to blogs or other documentation is welcome and appreciated.
As before, I'm very grateful for any help.