xstream 如今把版本由 1.3 升到 1.4 , 獲取的方式有點不一樣java
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 的時區設置xml
TimeZone zone = TimeZone.getTimeZone(CHINA_TIME_ZONE); XSTREAM.registerConverter(new DateConverter(zone), XStream.PRIORITY_NORMAL);
有時候 xtream 由xml 轉爲java對象時會報錯誤 AbstractReflectionConverter$UnknownFieldException對象
須要添加 xStream.autodetectAnnotations(true);blog
完整示例 : get
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)