Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 67544 invoked from network); 9 Feb 2009 13:31:43 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 9 Feb 2009 13:31:43 -0000 Received: (qmail 38223 invoked by uid 500); 9 Feb 2009 13:31:39 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 38153 invoked by uid 500); 9 Feb 2009 13:31:39 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 38141 invoked by uid 99); 9 Feb 2009 13:31:39 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Feb 2009 05:31:39 -0800 X-ASF-Spam-Status: No, hits=2.2 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of ahhughes@gmail.com designates 209.85.142.185 as permitted sender) Received: from [209.85.142.185] (HELO ti-out-0910.google.com) (209.85.142.185) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 09 Feb 2009 13:31:33 +0000 Received: by ti-out-0910.google.com with SMTP id j3so1611662tid.10 for ; Mon, 09 Feb 2009 05:31:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:content-type; bh=BBTb5d/Qoo/liNmqiK895JB7JWDAl0FsrCx+Nm73Vts=; b=ay6f9WF/psq0xRk+laHeuGLNYEpz8ENvWcGBK0jdWapmnaKuHqyGaUhORrgi2W+MPF SrUkYNtNxLXP4ApmOpPQF6tgIMXMejur8pCj0ZCuP+hE9S95qgnfcngj7pDXC8TNKU2F DpSAQb3Hkfipfb9WWKM7NEx2ROOp7cYeCz6sY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=aJBWSBrQUcjM8q5kxDtZh7U70yS0ihDVCB4D13Wsa5zyICsd/pBAkR8rT/sAhi3lLW yC2rpLLYIdwHqASD+1b/m/odqHrgYcGZ+/VtRmODW/0GyIz2X9Mwjn9t7g07WVi0Dbtn UpClCzzBGpmq/xlKqHbwIWnzg6o2IJivwX21c= MIME-Version: 1.0 Received: by 10.110.3.15 with SMTP id 15mr4799277tic.43.1234186272107; Mon, 09 Feb 2009 05:31:12 -0800 (PST) In-Reply-To: <5f528cf40902090354ge26bf58y2f9eab28c3ed6279@mail.gmail.com> References: <5f528cf40902081509h684b42a0hfb931335368dab01@mail.gmail.com> <173043.10551.qm@web55105.mail.re4.yahoo.com> <5f528cf40902090354ge26bf58y2f9eab28c3ed6279@mail.gmail.com> Date: Tue, 10 Feb 2009 00:01:12 +1030 Message-ID: <5f528cf40902090531n24fa6a4dwd469f63f1a1d0b1d@mail.gmail.com> Subject: Re: JXPath over Generic Collection, How? From: Andrew Hughes To: Commons Users List Content-Type: multipart/alternative; boundary=001485f4225e55ee7204627c624a X-Virus-Checked: Checked by ClamAV on apache.org --001485f4225e55ee7204627c624a Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit 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 { 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 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 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 wrote: >> >> > From: Andrew Hughes >> > Subject: JXPath over Generic Collection, How? >> > To: "Commons Users List" >> > 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{ >> > >> > 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 >> >> > --001485f4225e55ee7204627c624a--