回答
是。
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