commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject Re: [digester] accessing parent data and matching dynamic elements
Date Mon, 24 Nov 2003 22:23:00 GMT
On Tue, 2003-11-25 at 06:30, __matthewHawthorne wrote:
> I finally have found the chance to use Digester, and it's pretty 
> impressive.  However, there are a few issues that I'm confused about, 
> and would appreciate the help.
> Here's my XML:
> <table name="pmw">
>    <record id="1" name="pmw1"/>
>    <record id="2" name="pmw2"/>
>    <record id="3" name="pmw3"/>
> </table>
> <table name="por">
>    <record id="5" type="6"/>
>    ...
> </table>
> I'm using Digester to define my data to bulk load using commons-sql.
> for each <record>, I want to do:
> DynaBean bean = DynaSql.newInstance(../@name)
> and then set the record's attributes into the corresponding DynaBean.
> 1) How can I do this?  It seems that I can't access the parent element 
> to use it's "name" attribute.

Hi Matthew,

You can use a factory class to create your record objects (I think you
are already?]. This factory can access the Digester object stack to get
the object which represents the Table.

[nb: assuming your table tags are actually under a top-level element
called "root"]

digester.addObjectCreate("root/table", Table.class);
digester.addSetProperties("root/table"); # name->table.setName()

ObjectCreationFactory recordFactory = new RecordFactory();
digester.addFactoryCreate("root/table/record", recordFactory);

where the factory does
  Table table = (Table) digester.peek(0);
  return DynaSql.newInstance(table.getName());

This *does* require that there is some object to represent the Table. If
you try to "skip" an element in Digester, then try to reference the data
that it represents, things can get nasty. In your case, I think it makes
sense to have *something* represent the <table> anyway, right?
Otherwise, what holds the collection of records you are building?

Note that the FactoryCreateRule can take either an ObjectCreationFactory
Class or an object of type ObjectCreationFactory. The former just
creates an instance of the class then calls it. I prefer using the
latter form myself, but either works.

> 2) Originally, I tried doing it in this way:
> <pmw id="" name=""/>
> <por id="" name=""/>
> <anotherType id="" name=""/>
> But I couldn't find a pattern to match each element. In looking at the 
> examples, I'm thinking I could maybe do:
> Digester.addFactoryCreate("?", MyFactory.class) using ExtendedRulesBase
> or
> Digester.addFactoryCreate("*", MyFactory.class) using the RegexRules?

I presume your xml is actually like:
   <pmw name="xxx">
     <record id="1" name="pmw1"/>

ExtendedBaseRules can be used for "trailing wildcard matches", like
"root/*". I don't know personally whether that matches all elements
under root (including the record tags, which would be bad) or just
direct children.

I think RegexRules may help too, but I haven't used that myself.

> Sorry for the abundance of questions.  In the meanwhile, I'll keep 
> looking at the examples and doing experiments to try and figure this out.

If you find some stuff isn't sufficiently documented, please feel free
to either submit javadoc patches, or even better some new examples for
the examples dir in CVS! Or just post the list of stuff you had problems
with later & I will try to work up some new examples later.

I presume the "examples" you are referring to are the ones in CVS (but
not yet in an official release).

[By the way, no code above has been tested; reader beware :-]



> Thanks!
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message