commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rich Coco <rac...@starbak.com>
Subject Re: [Digester] basic usage problem. Help me understand the stack.
Date Tue, 27 Jul 2004 01:17:07 GMT
but what if a Part object could be member of other objects 
that are represented as xml elements, eg, consider an xmlrpc 
msg fragment:

<struct>
  <member>
    <name>Struct_1</name>
    <value>
      <array>
         <data>
           <value>
             <struct>
               <member>
                 <name>Struct_2</name>
.  .  .


Struct_1 can contain a Part element (not shown).
It also contains an array of Struct_2 elements,
one of whose members can either be a Part or another
structure containing a Part, etc...

Suppose you need to parse the xml and return, say, a hashtable
containing the associated java objects.

you may assume you know a priori the definitions for all 
structure types (in xml). and assume you even have alreay generated
Java class definition for each such structure. (not hard using digester
if you have an input spec (in xml of course) defining the strucures,
giving them a name, specifying the members and their types, etc...

it does not seem to me the "*/part" type of pattern will work.
this approach does not keep the correspondence between a <part> strucuture
and its parent container(s).

it's a gnarly problem me thinks...one i eventually have to solve.

- rich

Craig McClanahan wrote: 

> Actually, there's a fairly straightforward and elegant solution to
> this sort of problem, if you have control over the object types being
> constructed.  For example, assume you have something like this,
> representing a part (say, in a bill of materials explosion) that has
> sub-parts:
> 
>     package mypackage;
> 
>     import java.util.ArrayList;
>     import java.util.List;
> 
>     pubic class Part {
> 
>         // An "id" property for this part
>         private String partId;
>         public String getPartId() { return partId; }
>         pubic vod setPartId(String partId) { this.partId = partId; }
> 
>         // Add a new sub-part for this part
>         public void addPart(Part part) {
>             parts.add(part);
>         }
> 
>         // Retrieve the sub-parts for this part
>         private List parts = new ArrayList();
>         public List getParts() {
>             return parts;
>         }
> 
>     }
> 
> Now, you can construct a recursive hierarchy of parts with some very
> simple Digester rules (assuming you push onto the stack an empty part
> to collect all the top-level children):
> 
>     // Set up the digester
>     Digester digester = ...;
>     digester.addObjectCreate("*/part", "mypackage.Part");
>     digester.addSetProperties("*/part");
>     digester.addSetNext("*/part", "addPart", "mypackage.Part");
> 
>     // Push a dummy Part onto the stack to collect all the top-level parts
>     Part dummy = new Part();
>     digester.push(dummy);
> 
>     // Parse the XML document
>     digester.parse(...);
> 
>     // Now, process the top-level parts we parsed
>     Iterator parts = dummy.getParts().iterator();
>     while (parts.hasNext()) {
>         Part part = (Part) parts.next();
>         ...
>     }
> 
> And you can deal with arbitrarily nested part hierarchies:
> 
>     <parts>
>         <part ...>
>         </part>
>         <part ...>
>             <part .../>
>             <part .../>
>         </part>
>     </parts>
> 
>>good lucj
>>
>>- rich
> 
> 
> Craig
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message