[digester] handing attributes

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[digester] handing attributes

Markos Charatzas
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] handing attributes

Dan Tarkenton

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: [hidden email] For
> additional commands, e-mail: [hidden email]


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



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester] handing attributes

Rahul Akolkar
On 6/27/05, Daniel S. Tarkenton <[hidden email]> wrote:
<snip/>
> 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.

It gets passed in as a param to Rule#body(), save the lang attribute
while in CustomRule#begin() and retrieve it in CustomRule#body() to
call the appropriate setter.

-Rahul

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]