赞助

XML转换为对象/Javabean

將XML轉換為對象

 

 1 /**
 2      * xml转换成JavaBean
 3      * @param xml
 4      * @param c
 5      * @return
 6      */
 7     public static <T> T convertToJavaBean(String xml, Class<T> c) {
 8 
 9         if (StringUtils.isEmpty(xml)){
10             return null;
11         }
12         T t = null;
13         try {
14             JAXBContext context = JAXBContext.newInstance(c);
15             Unmarshaller unmarshaller = context.createUnmarshaller();
16             t = (T) unmarshaller.unmarshal(new StringReader(xml));
17         } catch (Exception e) {
18             log.error("xml to JavaBean ex.", e);
19         }
20 
21         return t;
22     }

 

posted @ 2018-11-09 17:02  念念不忘、  阅读(2540)  评论(0编辑  收藏  举报