回答
是。
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));
}
}
打印
<test>
<friends>
<person>
<name>Fred</name>
</person>
</friends>
<stuff>
<thing>
<description>Xbox 360</description>
</thing>
</stuff>
</test>
如果你的意思是别的东西,请澄清问题。
这将涉及到包装对象去。在你的情况下,'XStreamTest'。我在想,如果不这样做, – 2010-07-06 01:58:44
它也不是没有测试代码中的一个良好的XML文档有可能,不是吗? – I82Much 2010-07-06 02:42:33
你可以有朋友和东西列为单独的文件 – 2010-07-09 20:52:10
为什么不使用JAXB呢?如果你不喜欢的注解,你可以使用EclipseLink MOXy的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);
}
}
这将打印相同的XML作为XStream的例子:
<root>
<friends>
<friend>
<name>Fred</name>
</friend>
</friends>
<stuff>
<stuff>
<description>Xbox 360</description>
</stuff>
</stuff>
</root>
看看我的博客文章比较JAXB及XStream:http://bdoughan.blogspot.com/2010/10/how-does-jaxb-compare-to-xstream.html – 2010-10-07 18:45:33
- 1. XStream:使用XStream忽略集合
- 2. XStream的地图集合
- 3. XStream序列化集合
- 4. 使用XStream解析 - 空标记和集合
- 5. XStream的隐式集合配置文档
- 6. XStream - 作为对象集合的根
- 7. 通用集合
- 8. 使用XStream别名的集合的内容
- 9. 集合的通用类型子集合
- 10. 通用父集合拆分子集合
- 11. 在集合中创建通用集合
- 12. Magento通用集合
- 13. PowerShell通用集合
- 14. 通用集合类?
- 15. XStream和-XstartOnFirstThread
- 16. XStream和Proguard
- 17. 使用XStream和双逗号
- 18. 使用通用集合
- 19. XStream删除集合中不需要的字段
- 20. Xstream创建类并导致痛苦的permgen集合
- 21. TypeScript通用集合:列表
- 22. Java Collections.checked *()与通用集合
- 23. IValueConverter - 铸造通用集合
- 24. InvalidOperationException VB.Net通用集合?
- 25. vba中的通用集合?
- 26. 仿制药和Xstream
- 27. Java:通过引用收集集合的任意集合
- 28. 合并集合地图集合的通用方法
- 29. 如何通过使用multimap(apache集合类型)使用集合集合
- 30. 通用列表中的XStream例外
请参阅我的答案[这里](HTTP:/ /stackoverflow.com/questions/3136234/xstream-root-as-a-collection-of-objects/3138114#3138114)。 – chedine 2010-07-05 17:59:09
感谢,但这个例子是解组对象。我试图从对象到XML – 2010-07-05 18:12:27