Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 38016 invoked from network); 15 Jun 2006 11:35:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 15 Jun 2006 11:35:04 -0000 Received: (qmail 34428 invoked by uid 500); 15 Jun 2006 11:34:39 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 32735 invoked by uid 500); 15 Jun 2006 11:34:29 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 32553 invoked by uid 99); 15 Jun 2006 11:34:27 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Jun 2006 04:34:27 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: neutral (asf.osuosl.org: local policy) Received: from [210.54.141.240] (HELO fep06.xtra.co.nz) (210.54.141.240) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 15 Jun 2006 04:14:24 -0700 Received: from [10.1.1.8] (really [210.86.108.218]) by fep06.xtra.co.nz with ESMTP id <20060615111402.CQOU26685.fep06.xtra.co.nz@[10.1.1.8]> for ; Thu, 15 Jun 2006 23:14:02 +1200 Subject: Re: Using Digester to store info into a Map From: Simon Kitching To: Jakarta Commons Users List In-Reply-To: <20060612150403.24938.qmail@web30109.mail.mud.yahoo.com> References: <20060612150403.24938.qmail@web30109.mail.mud.yahoo.com> Content-Type: text/plain Date: Thu, 15 Jun 2006 23:14:07 +1200 Message-Id: <1150370048.8307.43.camel@blackbox> Mime-Version: 1.0 X-Mailer: Evolution 2.6.1 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Hi Jeff, On Mon, 2006-06-12 at 08:04 -0700, Jeff Marendo wrote: > Hello, > > Would anyone be able to tell me how to structure an XML rules file so that the Digester can create objects (from the example file below) and store them into a Map as opposed to a Collection? > > > > doej01 > > John > Doe > [snip] > > I want to store the User instance in a Map and use the value in the "id" element as the key. I assume you've really got something like: ... ... ... ... If user is the root level, then you've not got a lot of object in the map :-) This won't be trivial to do, because the key to use is embedded within the user element. Remember that Digester is just a simple layer over SAX, so it doesn't "look ahead" or "look behind"; data must be processed when it is encountered. The best solution would be to simply *not* use a map. If you instead declare: public class Users { private Map userMap = new HashMap(); public void addUser(User u) { userMap.put(u.getId(), u); } ... } then the solution is pretty obvious from there. If you are determined to use a primitive Map, then I think that you'll need to write a custom Rule class that knows how to add a User to a Map (ie implements the operation in the addUser method above). The rule's end method should assume that the top of stack contains a User, the second-to-top contains the target Map, and do the appropriate put call. Writing a custom Rule is really pretty easy and quite "normal"; Digester's built-in rules are really just a starting point. Unfortunately, invoking a custom rule when using the xmlrules module isn't easy. It's been a long time since I did it, so I can't even remember how it is done... It *might* be possible to get this working with just a CallMethodRule as documented in the FAQ on the wiki. However having the pattern for call parameters match stuff at a different level from the element the call occurs on can have unexpected interactions with other CallMethodRule rules; it's better avoided. http://wiki.apache.org/jakarta-commons/Digester/FAQ Regards, Simon --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org