How to use more than one @XStreamAlias annotation on same element. When I do like this:
@XStreamAlias("alias1")
@XStreamAlias("alias2")
class ABC{
//
}
I get compilation error. Is there any way to achieve this?
How to use more than one @XStreamAlias annotation on same element. When I do like this:
@XStreamAlias("alias1")
@XStreamAlias("alias2")
class ABC{
//
}
I get compilation error. Is there any way to achieve this?
Here is example with programmatic way:
import java.io.InputStream;
import java.util.List;
import org.junit.Test;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.Data;
public class MultiAlias {
@Test
public void smokeTest() {
InputStream file = MultiAlias.class.getResourceAsStream("MultiAliasTest.xml");
XStream xStream = new XStream();
xStream.ignoreUnknownElements();
xStream.processAnnotations(Parent.class);
xStream.alias("child", Child.class);
xStream.alias("kind", Child.class);
Parent parent = (Parent) xStream.fromXML(file);
System.out.println(parent);
}
@XStreamAlias("parent")
@Data
public class Parent {
@XStreamAsAttribute
private String name;
private List<Child> children;
}
@Data
public class Child {
private String name;
}
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
<parent name="Adam">
<age>33</age>
<children>
<child>
<name>Iva</name>
</child>
<kind>
<name>Peter</name>
</kind>
</children>
</parent>
Output:
MultiAlias.Parent(name=Adam, children=[MultiAlias.Child(name=Iva), MultiAlias.Child(name=Peter)])
I think, as Mark says, that is not possible with annotations (can't find anything in the web). In my particular case, as a walk arround, what I did was to extend from the base class (ABC in the Vivek example) and put their own annotation in the child classes and use those classes. Bud the solution depends on your code implementation. Regards.-
ABC class
@XStreamAlias("alias1") DFG extends ABC
@XStreamAlias("alias2") XYZ extends ABC