xstream does not apply dynamic binding when converting XML to Javabean

I am using XStream to convert XML to javabean.
Here’s a example of the XML I am trying to process:

<productlist>
    <a>
       <name>a1</name>
    </a>
    <a>
       <name>a2</name>
    </a>
    <b>
       <name>b1</name>
    </b>
    <b>
       <name>b2</name>
    </b>
</productlist>

I used an abstact Product and set A and B as two subclasses of it.
It works well when I want to use it to convert from javabean to XML, as I use a List and add instances of A or B to it. XStreamAlias works fine, too.

However, when I need to use it to transfer from XML to javabean, it does not work. Seems that XStream cannot automatically identify the subclass and put it into a List.

Now I have to create a class ProductList, put lists List and List into it and use @XStreamImplicit to ignore the list names. It does work, but if new subclasses C is created, I will have to modify ProductList and add a List. It is surely not a good way of coding.

Is there a better solution of doing this? Say using some certain annotations or what?


Source: xml

Leave a Reply