Hi Paul,
Paul B. Anderson wrote:
> I managed to create three almost identical converters for the three
> ArrayList<String> elements of my Java structure and I can both read and
> write XML in the desired format.
>
> I was unable to see how to get the CollectionConverter or
> NamedCollectionConverter to fit in. I don't have distinct types for the
> three lists and the examples I saw assumed a distinct type for the
> collection.
?? You register an individual instance of the NamedCollectionConverter as
local converter directly for each list ... why do you assume you have to
have distinct types?
>
> I was trying to use the annotation approach because that seemed to be
> the only way to be specific about which of the ArrayLists I was
> referring to. It was not clear how to proceed and trial and error was
> not effective for me.
>
> What I would really like is an annotation or combination of annotations
> that combines a top level alias with an implicit alias, something like
>
> @XStreamCollection("movies", "title")
> ArrayList<String> movies;
>
> @XStreamCollection("plays", "title")
> ArrayList<String> plays;
@XStreamConverter(value=NamedCollectionConverter.class, strings={"title"},
types={String.class})
ArrayList<String> movies;
@XStreamConverter(value=NamedCollectionConverter.class, strings={"title"},
types={String.class})
ArrayList<String> plays;
produces:
> etc., producing
> ...
> <movies>
> <title>Title 1</title>
> <title>Title 2</title>
> </movies>
> <plays>
> <title>Title 1</title>
> <title>Title 2</title>
> </plays>
This XML is *not* implicit, since you have a surrounding tag for your
elements - at least this is what *implicit* everywhere means for XStream.
> It seems to me that simple collections or maps are somewhat special. If
> it were more complex than a simple collection, a person would typically
> want to develop a class but for just a list of items a special class is
> much less compelling.
You don't have to. The alternative would have been to register the local
converters using the XStream facade. Funny enough, the converter instance
can be the same here, because the name and type of the items are the same
(assuming Foo is the name of the class containing those two lists):
Converter namingConverter = new NamedCollectionConverter(
xstream.getMapper(), "title", String.class);
xstream.registerLocalConverter(Foo.class, "movies", namingConverter);
xstream.registerLocalConverter(Foo.class, "plays", namingConverter);
Cheers,
Jörg
---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email