commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Hughes <ahhug...@gmail.com>
Subject Re: JXPath over Generic Collection<?>, How?
Date Mon, 09 Feb 2009 13:52:54 GMT
I've noew tested a whole heap of expressions even //name='Tiger' nothing
works.
On Tue, Feb 10, 2009 at 12:01 AM, Andrew Hughes <ahhughes@gmail.com> wrote:

> I got cracking on testing this out... no luck (yet). Here's my testing code
> if some kind person could please take a look....
> First my generic collection hierarchy (which only contains a name and
> children)...
>
> package jxpathresearch;
>
> import java.util.ArrayList;
>
> public class HierarchyPojo extends ArrayList<HierarchyPojo> {
>
>     public HierarchyPojo(String name){
>         this.setName(name);
>     }
>     private String name = "";
>
>     public String getName() {
>         return name;
>     }
>     public void setName(String name) {
>         this.name = name;
>     }
>
> }
>
>
> Next, the wrapper for the root context (as Matt suggested) and populated
> with animals...
>
> package jxpathresearch;
>
>
> public class CollectionRoot {
>
>
>     private HierarchyPojo hierarchyPojo;
>
>
>     public CollectionRoot(){
>
>         //Animal
>
>         hierarchyPojo = new HierarchyPojo("Animal");
>
>         //Animal.Dog
>
>         HierarchyPojo dog = new HierarchyPojo("Dog");
>
>         //Animal.Dog.Labrador
>
>         dog.add(new HierarchyPojo("Labrador"));
>
>         //Animal.Dog.Boxer
>
>         dog.add(new HierarchyPojo("Boxer"));
>
>         //Animal.Dog.Mastiff
>
>         dog.add(new HierarchyPojo("Mastiff"));
>
>         //Animal.Cat
>
>         HierarchyPojo cat = new HierarchyPojo("Cat");
>
>         //Animal.Cat.Tiger
>
>         cat.add(new HierarchyPojo("Tiger"));
>
>         //Animal.Cat.Cougar
>
>         cat.add(new HierarchyPojo("Cougar"));
>
>         //Animal.Cat.Leopard
>
>         cat.add(new HierarchyPojo("Leopard"));
>
>         //Add Animal.Dog & Animal.Cat
>
>         hierarchyPojo.add(dog);
>
>         hierarchyPojo.add(cat);
>
>     }
>
>
>     public HierarchyPojo getHierarchyPojo() {return hierarchyPojo;}
>
>
>     public void setHierarchyPojo(HierarchyPojo hierarchyPojo)
> {this.hierarchyPojo = hierarchyPojo;}
>
>
> }
>
>
> Finally invoke and test...
>
> public class App
>
> {
>
>     public static void main( String[] args )
>
>     {
>
>         JXPathContext context = JXPathContext.newContext(new
> CollectionRoot());
>
>         String query = "//hierarchyPojo[@name='Tiger']";
>
>         String fName = context.getValue(query).toString();
>
>         System.out.println("Ran '"+query+"' and got '"+fName+"'");
>
>     }
>
> }
>
>
>
> Above, should find one entry for 'name=Tiger' but it does not, I get an
> exception. This still doesn't seem to traverse the Collection correctly. Any
> help would be most welcome.
> Exception in thread "main"
> org.apache.commons.jxpath.JXPathNotFoundException: No value for xpath:
> //hierarchyPojo[@name='Tiger']
>
> Thanks for reading,
> Andrew
>
>
>
> On Mon, Feb 9, 2009 at 10:24 PM, Andrew Hughes <ahhughes@gmail.com> wrote:
>
>> Thanks Matt - I will test this out tomorrow when I am back in the
>> office...
>>
>> Being constructive...
>> Surely this should at least be a precondition check and throw a specific
>> exception if it's not supported?
>> Thank You
>> --Andrew
>>
>>
>> On Mon, Feb 9, 2009 at 2:27 PM, Matt Benson <gudnabrsam@yahoo.com> wrote:
>>
>>>
>>> Most likely your problem is not with generics, but simply with the fact
>>> that JXPath has a hard time using a collection as its root.  The easiest
>>> workaround is to use some parent object to hold a reference to your
>>> container.
>>>
>>> HTH,
>>> Matt
>>>
>>>
>>> --- On Sun, 2/8/09, Andrew Hughes <ahhughes@gmail.com> wrote:
>>>
>>> > From: Andrew Hughes <ahhughes@gmail.com>
>>> > Subject: JXPath over Generic Collection<?>, How?
>>> > To: "Commons Users List" <user@commons.apache.org>
>>> > Date: Sunday, February 8, 2009, 5:09 PM
>>> > Hi All,
>>> > Hopefully the solution is as easy as the question. I would
>>> > like to perform
>>> > evaluation on a (very simple) generic collection... as you
>>> > can see below
>>> > (HeirarchyPojo). I should be able to ask for a
>>> > HeirarchyPojo's with
>>> > name='Bill' or the 3rd Child... The problem is that
>>> > nothing ever evaluate on
>>> > this data structure. What's the deal with Generic
>>> > Collections and JXPath?
>>> >
>>> > p.s this is not in the userguide and would be a most
>>> > welcomed addition (if
>>> > we can nut this out with your help).
>>> >
>>> > Cheers.
>>> >
>>> >
>>> > package xpath.and.generics;
>>> >
>>> > import java.util.ArrayList;
>>> >
>>> > public class HeirarchyPojo extends
>>> > ArrayList<HeirarchyPojo>{
>>> >
>>> >     public HeirarchyPojo(){}
>>> >
>>> >     private String id;
>>> >     private String name;
>>> >
>>> >     public String getId() {
>>> >         return id;
>>> >     }
>>> >
>>> >     public void setId(String id) {
>>> >         this.id = id;
>>> >     }
>>> >
>>> >     public String getName() {
>>> >         return name;
>>> >     }
>>> >
>>> >     public void setName(String name) {
>>> >         this.name = name;
>>> >     }
>>> >
>>> > }
>>>
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
>>> For additional commands, e-mail: user-help@commons.apache.org
>>>
>>>
>>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message