commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <>
Subject Re: [digester] Setting arbitrary XML attributes
Date Tue, 23 Nov 2004 03:11:20 GMT
Hi Lance,

On Tue, 2004-11-23 at 15:59, Lance Semmens wrote:
> I'm wanting to set all XML attributes on my object.
> At the time of XML digesting, I don't know the set of attribute names.
> If I use a SetPropertiesRule, my object will require a setX() method for
> every attribute.
> If I parse
> 	<mybean foo="ABC" bar="123" />
> Using the following bean:
> 	public class MyBean {
> 		public Map attributes = new HashMap();
> 		public void setAttribute(String name, String value);
> 	}
> I'd like the digester to do the following:
> 	MyBean myBean = new MyBean();
> 	myBean.setAttribute("foo", "ABC");
> 	myBean.setAttribute("bar", "123");
> Is there a digester rule that will do this?

There's no standard Rule class distributed with Digester that does this,
but implementing your own subclass of Rule to do this is very easy.


public class MyBeanAttributeSetterRule 
   extends org.apache.commons.digester.Rule
   public void begin(String namespace, String name, Attributes attrs)
     MyBean b = (MyBean) digester.peek();
     for each attribute in attrs
       call setAttribute(name, value)

Then of course:
   digester.addRule(somePattern, new MyBeanAttributeSetterRule());

Of course it would be possible to use reflection etc to avoid direct
coupling between the new Rule class and the MyBean class, but there's
probably not much point.



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

View raw message