将XML转换为Java DTO并返回GWT

时间:2010-03-15 22:17:09

标签: java xml gwt jaxb dto

寻找使用GWT将Java DTO转换为XML并返回的最佳方法。我看到GWT在其客户端包中有XMLParser,它是一个DOM Parser。我正在寻找更像JAXB类型的插件功能,我可以使用GWT。

1 个答案:

答案 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());
    }

}