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     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM