gpt4 book ai didi

java - 通用集合和 XStream

转载 作者:行者123 更新时间:2023-11-30 05:11:35 24 4
gpt4 key购买 nike

有没有办法映射(使用xstream)List<Person><friends>List<Things><stuff>例如?

谢谢!

最佳答案

是的。

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>

如果您有其他意思,请澄清问题。

关于java - 通用集合和 XStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3181323/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com