xstream 的一点笔记

xstream 现在把版本由 1.3 升到 1.4 , 获取的方式有点不同

		XStream xStream = new XStream(new XppDriver(new XmlFriendlyReplacer("_-", "_")));//xtream 1.3
//		XStream xStream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));//xtream 1.4
		SystemMonitorInfo info = new SystemMonitorInfo();
		xStream.processAnnotations(SystemMonitorInfo.class);
		System.out.println(xStream.toXML(info));

  

xtream 的时区设置

        TimeZone zone = TimeZone.getTimeZone(CHINA_TIME_ZONE);
        XSTREAM.registerConverter(new DateConverter(zone), XStream.PRIORITY_NORMAL);

  

有时候 xtream 由xml 转为java对象时会报错误 AbstractReflectionConverter$UnknownFieldException

需要添加 xStream.autodetectAnnotations(true);

完整示例 : 

		XStream xStream = new XStream(new DomDriver("UTF-8", new XmlFriendlyNameCoder("-_", "_")));//xtream 1.4

		xStream.autodetectAnnotations(true);
		xStream.setClassLoader(SystemMonitorInfo.class.getClassLoader());
        
		xStream.alias("root",SystemMonitorInfo.class);
		SystemMonitorInfo monitor = (SystemMonitorInfo)xStream.fromXML(monitorXml)

  

 

posted on 2019-04-02 14:28  hi-gdl  阅读(410)  评论(0编辑  收藏  举报

导航