commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject RE: Digester beginner question
Date Tue, 26 Apr 2005 05:40:41 GMT
Hi,

Yep, I pretty much agree with Lance.

Digester doesn't support xpath-like matching; it would be nice but is
difficult and no-one has yet done so.

Processing xml like
  <attribute name="id">some-value</attribute>
is awkward because info on the target field ("id") is provided in a
different callback from the data.

Lance's solution looks ok to me. It may not work right if the rule is
associated with a wildcard pattern (eg "*/attribute") - if you need
this, I would suggest replacing fieldName with a stack, or using the
digester "named stacks" feature.

And if your Student class has setter/getter methods to access the fields
(which is *highly* recommended) then it would be better to use
        BeanUtils.setProperty(o, fieldName, text);
Of course if your Student class directly exposes fields as per your
example, then Lance's code is exactly right. One last thing: if your
Student class really has *package-scope* members, then the Rule class
will need to be in the same package as the Student class in order to
access those fields - public getter/setter methods are much nicer.

Regards,

Simon

On Tue, 2005-04-26 at 12:04 +0930, Lance Semmens wrote:
> I can't think of an existing rule that will help you.
> How's this?? (NB i have not tested but you should get the idea).
> 
> digester.addRule("class/student/attribute" new Rule() {
> 	private String fieldName;
> 	public void begin(Attributes atts) throws Exception {
> 		fieldName = atts.getValue("name");
> 	}
> 	public void body(String text) throws Exception {
> 		Object o = getDigester().peek();
> 		Field field = o.getClass().getField(fieldName);
> 		field.set(o, text);
> 	}
> }
> 
> -----Original Message-----
> From: Manoj Chakkalakkal [mailto:Manoj.Chakkalakkal@semagix.com]
> Sent: Tuesday, 26 April 2005 7:14 AM
> To: Jakarta Commons Users List
> Subject: RE: Digester beginner question
> Importance: Low
> 
> 
> I tried using the XPath notation as shown, but that does not work.
> 
> Digester digester = new Digester();
> digester.addObjectCreate("class", Student.class);
> digester.addBeanPropertySetter("class/student/attribute[@name='id']",
> "id");
> digester.addBeanPropertySetter("class/student/attribute[@name='name']",
> "name");
> 
> But that does not seem to work.
> 
> 
> -----Original Message-----
> From: Manoj Chakkalakkal 
> Sent: Monday, April 25, 2005 5:41 PM
> To: Jakarta Commons Users List
> Subject: Digester beginner question
> 
> I have the following XML.
> <class>
>  	<student>
>   		<attribute name="id">10</attribute>
> 		<attribute name="name">Mark</attribute>
> 	</student>
> 	<student>
>   		<attribute name="id">11</attribute>
> 		<attribute name="name">Mary</attribute>
> 	</student>
> 
> </class>
> 
> public class Student{
> 	String id;
> 	String name;
> }
> 
> How do I write use Commons Digester to set the properties id and name of
> the class Student?
> 
> I cannot change the structure of XML.
> 
> Thank you
> -Manoj



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