commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Carsten Kaiser" <Carsten.Kai...@valtech.de>
Subject RE: [digester] Setting arbitrary XML attributes
Date Tue, 23 Nov 2004 08:08:22 GMT
Wouldn't it be also possible just to let MyBean implement the DynaBean interface or directly
uses the BasicDynaBean itself? If so, BeanUtils
should properly populate your attributes! I guess, it is exactly, what you are looking for...

Regards,
CAK

-----Urspr√ľngliche Nachricht-----
Von:	Lance Semmens [mailto:lance.semmens@essential.com.au]
Gesendet:	Di 23.11.2004 04:13
An:	'Jakarta Commons Users List'
Cc:	
Betreff:	RE: [digester] Setting arbitrary XML attributes
Thanks Simon.
I was hoping that the reflective option existed rather than creating a
specialized rule.
I may look at writing the reflective option myself.

Lance.

-----Original Message-----
From: Simon Kitching [mailto:simon@ecnetwork.co.nz]
Sent: Tuesday, 23 November 2004 1:41 PM
To: Jakarta Commons Users List
Subject: Re: [digester] Setting arbitrary XML attributes
Importance: Low


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.

Roughly:

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.

Regards,

Simon


---------------------------------------------------------------------
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