寻找使用GWT将Java DTO转换为XML并返回的最佳方法。我看到GWT在其客户端包中有XMLParser,它是一个DOM Parser。我正在寻找更像JAXB类型的插件功能,我可以使用GWT。
答案 0 :(得分:2)
我非常喜欢XStream,但它只适用于GWT服务器端
package xstream;
import com.thoughtworks.xstream.XStream;
import java.util.HashMap;
import java.util.Map;
public class XstreamUtil {
private static Map streams= new HashMap();
public static String toXml(Object o) {
return getStream(o.getClass()).toXML(o);
}
public static T fromXml(Class c, String xml) {
return (T) getStream(c).fromXML(xml);
}
public static T copyOf(Class clazz, Object o) {
return (T) getStream(clazz).fromXML(getStream(o.getClass()).toXML(o));
}
private static synchronized XStream getStream(Class c) {
if (!streams.containsKey(c.getPackage())) {
XStream x = new XStream();
streams.put(c.getPackage(), x);
}
return (XStream) streams.get(c.getPackage());
}
}