commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel S. Tarkenton" <d...@tarksoft.com>
Subject Re: [digester] handing attributes
Date Mon, 27 Jun 2005 14:43:06 GMT

You can accomplish this by creating a custom Rule class.  To capture data
based on attribute type, you can try:

class CustomRule extends Rule {

	public CustomRule(Digester digester) {

        super();
        this.digester = digester;
    }

	public void begin(String namespace, String elementName, Attributes attrs) {
	    String nameAttr = attrs.getValue("lang");
	    if (nameAttr.equals("eng")) {


	      ValueHolder myValHolder = (ValueHolder) digester.peek();
	      //get body text
	      myValHolder.setEnglishName(bodyText);
            } if (nameAttr.equals("el")) {
              ValueHolder myValHolder = (ValueHolder) digester.peek();
              //get body text
	      myValHolder.setGreekName(bodyText);
            }
	  }
}

Then wherever you initialize your digester object, you want code that
looks something like this:

digester.addRule("type/names/name", new CustomRule(digester));

Keep in mind that you may actually need to implement the body method as
well (to get the data between the start and end element) -- I'm not 100%
positive as to how to accomplish my comment of "//get body text". 
Hopefully this will get you started to a degree.

> Hi there,
>
> Is there a way to have an xml file of,
>
> <type>
> 	<names>
> 		<name lang="en">english name</name>
> 		<name lang="el">greek name</name>
> 	</names>
> </type>
>
> and map it to a bean
>
> Type
> setGreekName(String)
> setEnglishName(String)
>
> passing the correct parameter depending on the attribute value?
>
> As far as I can see from the docs this is not supported :/
>
> Thanks,
> Markos
>
> --------------------------------------------------------------------- To
> unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For
> additional commands, e-mail: commons-user-help@jakarta.apache.org


-- 
Daniel S. Tarkenton
21504 Welby Terrace
Ashburn, VA  20148
(703)994-2187



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