Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 77644 invoked from network); 27 Jul 2004 01:54:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 27 Jul 2004 01:54:48 -0000 Received: (qmail 69042 invoked by uid 500); 27 Jul 2004 01:54:40 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 68937 invoked by uid 500); 27 Jul 2004 01:54:39 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: 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 68921 invoked by uid 99); 27 Jul 2004 01:54:39 -0000 X-ASF-Spam-Status: No, hits=1.3 required=10.0 tests=RCVD_BY_IP,SB_NEW_BULK,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received: from [64.233.170.207] (HELO mproxy.gmail.com) (64.233.170.207) by apache.org (qpsmtpd/0.27.1) with ESMTP; Mon, 26 Jul 2004 18:54:35 -0700 Received: by mproxy.gmail.com with SMTP id 74so97555rnl for ; Mon, 26 Jul 2004 18:54:34 -0700 (PDT) Received: by 10.38.81.69 with SMTP id e69mr298518rnb; Mon, 26 Jul 2004 18:54:34 -0700 (PDT) Message-ID: Date: Mon, 26 Jul 2004 18:54:34 -0700 From: Craig McClanahan To: Jakarta Commons Users List Subject: Re: [Digester] basic usage problem. Help me understand the stack. In-Reply-To: <1090891440.7762.41.camel@pcsimon> Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit References: <7dd64355040726141575887d73@mail.gmail.com> <41058870.9010302@starbak.com> <4105AD13.6090902@starbak.com> <1090891440.7762.41.camel@pcsimon> X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N On Tue, 27 Jul 2004 13:24:00 +1200, Simon Kitching wrote: > On Tue, 2004-07-27 at 13:17, Rich Coco wrote: > > > > Suppose you need to parse the xml and return, say, a hashtable > > containing the associated java objects. > > In other words you want to flatten the hierarchy? That doesn't seem like something you *have* to do while parsing (if you do, then you might need some custom Digester rules -- doable, but more work). Instead, assuming the Part data structure that I identified in my earlier response, you can create a flattened Map after the parse completes, like this: Part dummy = ... the dummy part from earlier ... Map flattened = new HashMap(); Iterator parts = dummy.getParts().iterator(); while (parts.hasNext()) { flatten(flattened, (Part) parts.next()); } // Private method that recursively flattens a part structure private void flatten(Map map, Part part) { // Add this part to the map map.put(new Integer(part.getId()), part); // Add the sub-parts of this part to the map recursively Iterator parts = part.getParts(); while (parts.hasNext()) { flatten(map, (Part) parts.next()); } } With an extra "if" statement, you can do things like check for duplicate part ids, if that is needed. The extra loop in the main body of the code is based on the assumption that the dummy top-level Part isn't a "real" Part ... it's just a collector. If it is real (in other words, if the entire BOM you are parsing is to construct a single assembly), the main line code is even simpler: Part dummy = ... the dummy part from earlier ... Map flattened = new HashMap(); flatten(flattened, dummy); Craig --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org