4

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.

1 Answers1

0

if you have the trip instance, why can't you do this

 string n = trip.name;

I can see that you have an XML representation of the trip, but using XPath would be for when you only have the XML for 'trip'.

If you still need to get name using XPath, post the XML that is generated. I would be willing to bet that the name attribute is not part of Trip, but part of the enclosing BaseDomain. If you are basing the jxPathContext on the Trip, you've already passed the nodes for the BaseDomain. You'd have to navigate back up somehow (like node.parent) or do that when you create the context,

// not sure about this approach
JXPathContext jxPathContext = JXPathContext.newContext(trip.super); 

After looking at the JXPath manual on parent/child relationships why don't you try this:

String name = (String)jxPathContext.getValue("/Trip/../@name");    
// or use "../@name", not sure it wouldn't need to be "/../@name"
Kelly S. French
  • 12,198
  • 10
  • 63
  • 93
  • Thanks for the reply. I'll try the trip.super solution. Do you know if there is a way of using .super inside the jxpath-query? – Max Sjögren Feb 12 '13 at 20:59
  • Sad to say that the .super solution does not work. It results in an error before compiling stating that "trip cannot be resolved to a type". – Max Sjögren Feb 13 '13 at 07:32