On Sun, 2006-09-03 at 12:43 -0300, Agustin Barto wrote:
> I need to parse an XML like this:
> 
> <message-list>
>   <message>Message1</message>
>   <message>Message2</message>
>   ...
>   <message>MessageN</message>
> </messge-list>
> 
> which should render a List<String> (or at least Collection<String>).
> Right now what I do is this:
> 
>       @SuppressWarnings("unchecked")
>       protected static List<String> parseMessageList(String uri) throws
> IOException, SAXException {
>               Digester digester = new Digester();
>               
>               digester.setValidating(false);
>               digester.addObjectCreate("message-list", ArrayList.class );
>               digester.addCallMethod("message-list/message", "add", 1, new 
> Class[]
> { String.class });
>               digester.addCallParam("message-list/message", 0);
>               
>               return (List<String>) digester.parse(uri);
>       }
> 
> ...and I was wondering if it's the right way to do it, and if not, if
> there is a better way.

This looks pretty good to me.

Minor issues:
(a)
I think you can leave out the "new Class[] {String.class}". Digester
should be able to figure that out itself.
(b)
If you do
    digester.addCallMethod("message-list/message", "add", 0);
then the add method should be called with 1 string param: the element
body. You can therefore get rid of the addCallParam line. Yes, this
isn't the most obvious API, but it's that way for historical purposes
and it is documented in the CallMethodRule. 

Optionally, instead of creating an ArrayList element at <message-list>,
you could just push an instance onto the digester stack before starting
to parse.

Actually, your existing code is more obvious (though not quite as fast)
so it might be better to keep it exactly as it currently is.

If you haven't already seen the digester examples, I recommend you
download the Digester source distribution and have a look.

Regards,

Simon



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to