这一定是一个新手问题,但我无法从
http://x-stream.github.io/获得它.
好吧,我有以下xml字符串
<cat age="4" >
<name>Garfield</name>
</cat>
需要映射到:
class Cat {
int age;
String name;
}
使用XStream有一种简单的方法吗?如果没有,我还能尝试什么?
提前致谢.
最佳答案 像这样注释您的类(有关详细信息,请查看
http://x-stream.github.io/annotations-tutorial.html):
@XStreamAlias("cat")
class Cat {
@XStreamAsAttribute
int age;
String name;
}
现在只需按如下方式使用XStream:
xstream = new XStream();
xstream.processAnnotations(Cat.class);
Cat roundtripGarfield = (Cat)xstream.fromXML(xstream.toXML(garfield));