JQPath query attribute in superclass

I have a couple of domain classes that inherit from BaseDomain class.

In this BaseDomain class, I have a field that is the public final name of the String;

The name value is set in the constructor.

public class BaseDomain {
    public final String name;
    public BaseDomain() {
    this.name = this.getClass().getCanonicalName();
    }
}

      

This BaseDomain has been extended with several 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 "name" field in the Trip a instance using JXPath, but I 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 "name" with this setting and JXPath?

There may be some syntax errors and / or other errors in the code. I hope you all understood and understood my question.

Any help or pointer is greatly appreciated.

First, I want to thank Kelly S. French for his quick replay. This made me realize that there is something else I must explain.

I want to use jxpath because I will end up doing a deeper search. For example: "Trip" can contain a list of places that also extend BaseDomain. Each location can contain a PointOfInterest list that extends BaseDomain.

Through reflection in another piece of code, I want to get a list of BaseDomains based on their type (class.getCanonicalName ())

Object tree is not xml based, it is pure POJO.

As far as I understood, there is no way to write a jxpath query to find a list of objects based on their type, class name, etc.

It is right? Does anyone know how to do this?

The easiest way out, even if ugly, is to have a field in the superclass that contains the class name. That's why I made this ugly solution.

Eventually I want to create a jxpath query that, based on the trip, returns an iterator from which there has ever been an object being 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 for the object I'm looking for.

Does anyone know if it is possible to achieve this with a jxpath query?

Sample code, links to blogs, or other documentation are welcome and appreciated.

As before, I am very grateful for any help.

+3


source to share


1 answer


if you have a copy of the trip why can't you do it

 string n = trip.name;

      

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

If you still need to get the name using XPath, post the generated XML. I would be willing to bet that the name attribute is not part of Trip, but part of the supplied BaseDomain. If you are using jxPathContext on Trip, you have already passed BaseDomain nodes. You will need to back up in some way (e.g. node.parent) or do this when creating the context,



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

      

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

String name = (String)jxPathContext.getValue("/Trip/../@name");    
// or use "../@name", not sure it wouldn't need to be "/../@name"

      

0


source







All Articles