Is there a way to map (using xstream) a List<Person>
to <friends>
and List<Things>
to <stuff>
for example?
Thanks!
Is there a way to map (using xstream) a List<Person>
to <friends>
and List<Things>
to <stuff>
for example?
Thanks!
Yes.
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author nicholasdunn
*/
public class XStreamTest {
private List<Person> friends = new ArrayList<Person>();
private List<Thing> stuff = new ArrayList<Thing>();
public XStreamTest(List<Person> people, List<Thing> stuff) {
this.friends.addAll(people);
this.stuff.addAll(stuff);
}
private static class Person {
private String name;
public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private static class Thing {
private String description;
public Thing(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
};
public static void main(String[] args) {
XStream xstream = new XStream(new DomDriver());
xstream.alias("test", XStreamTest.class);
xstream.alias("person", Person.class);
xstream.alias("thing", Thing.class);
XStreamTest test = new XStreamTest(Arrays.asList(new Person("Fred")), Arrays.asList(new Thing("Xbox 360")));
System.out.println(xstream.toXML(test));
}
}
Prints
<test>
<friends>
<person>
<name>Fred</name>
</person>
</friends>
<stuff>
<thing>
<description>Xbox 360</description>
</thing>
</stuff>
</test>
If you mean something else, please clarify the question.
Why not use JAXB instead? If you don't like the annotations you can use EclipseLink MOXy's XML metata representation:
import java.util.Arrays;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
public class JAXBTest {
@XmlRootElement
public static class Root {
private List<Person> friend;
private List<Thing> stuff;
@XmlElementWrapper(name="friends")
public List<Person> getFriend() {
return friend;
}
public void setFriend(List<Person> friend) {
this.friend = friend;
}
@XmlElementWrapper(name="stuff")
public List<Thing> getStuff() {
return stuff;
}
public void setStuff(List<Thing> stuff) {
this.stuff = stuff;
}
}
public static class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public static class Thing {
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
public static void main(String[] args) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Root.class);
Root root = new Root();
Person fred = new Person();
fred.setName("Fred");
root.setFriend(Arrays.asList(fred));
Thing xbox = new Thing();
xbox.setDescription("Xbox 360");
root.setStuff(Arrays.asList(xbox));
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
This prints the same XML as the XStream example:
<root>
<friends>
<friend>
<name>Fred</name>
</friend>
</friends>
<stuff>
<stuff>
<description>Xbox 360</description>
</stuff>
</stuff>
</root>