title: jXPath quering attribute in super class
body: <p>I have a couple of domain classes which all inherit a BaseDomain class.</p>
  
  <p>In this BaseDomain class I have a field that is public final String name;</p>
  
  <p>The value of name is set in the constructor.</p>
  
  <pre><code>public class BaseDomain {
      public final String name;
      public BaseDomain() {
      this.name = this.getClass().getCanonicalName();
      }
  }
  </code></pre>
  
  <p>This BaseDomain is extended by a few classes</p>
  
  <pre><code>public class Trip extends BaseDomain {
      private int id;
      public Trip(int id){
          this.id = id;
      }
  
  }
  </code></pre>
  
  <p>So far so good.</p>
  
  <p>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.</p>
  
  <pre><code>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. 
  </code></pre>
  
  <p>Is it possible to get the value of "name" with this setup and JXPath?</p>
  
  <p>The code might have some syntax errors and/or other errors. I hope you all get the idea and understand my question. </p>
  
  <p>Any help or pointer are welcome.</p>
  
  <p>First of: I want to thank Kelly S. French for his quick replay.
  It made me realize that I have to explain some more.</p>
  
  <p>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.</p>
  
  <p>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())</p>
  
  <p>The object-tree is not based on xml, it is pure POJO.</p>
  
  <p>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.</p>
  
  <p>Is this correct?
  Does some one know of a way to do that?</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>Does any one know if it is possible to achive this with a jxpath-query?</p>
  
  <p>Code example, links to blogs or other documentation is welcome and appreciated.</p>
  
  <p>As before, I'm very grateful for any help.</p>