zoukankan      html  css  js  c++  java
  • 把xml转成javabean的工具类

    import java.io.IOException;
    import java.io.StringReader;
    import java.io.StringWriter;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.JAXBException;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    import com.alipayeco.medicalinscore.common.Global;
    
    public class JaxbUtil {//工具类
    
        /**
         * java对象转换为xml文件
         * @param xmlPath  xml文件路径
         * @param load    java对象.Class
         * @return    xml文件的String
         * @throws JAXBException    
         */
        public static String beanToXml(Object obj, Class<?> load) throws JAXBException {
            JAXBContext context = JAXBContext.newInstance(load);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
            marshaller.setProperty(Marshaller.JAXB_ENCODING, Global.ENCODING);
            StringWriter writer = new StringWriter();
            marshaller.marshal(obj, writer);
            return writer.toString();
        }
    
        /**
         * xml文件配置转换为对象
         * @param xmlPath  xml文件路径
         * @param load    java对象.Class
         * @return    java对象
         * @throws JAXBException    
         * @throws IOException
         */
        @SuppressWarnings("unchecked")
        public static <T> T xmlToBean(String xmlPath, Class<T> load) throws JAXBException, IOException {
            JAXBContext context = JAXBContext.newInstance(load);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (T) unmarshaller.unmarshal(new StringReader(xmlPath));
        }
        
        /** 
         * JavaBean转换成xml 
         * 默认编码UTF-8 
         * @param obj 
         * @param writer 
         * @return  
         */
        public static String convertToXml(Object obj) {
            //       return convertToXml(obj, "UTF-8");  
            return convertToXml(obj, "UTF-8");
        }
    
        /** 
         * JavaBean转换成xml 
         * @param obj 
         * @param encoding  
         * @return  
         */
        public static String convertToXml(Object obj, String encoding) {
            String result = null;
            try {
                JAXBContext context = JAXBContext.newInstance(obj.getClass());
                Marshaller marshaller = context.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
                
                StringWriter writer = new StringWriter();
                marshaller.marshal(obj, writer);
                result = writer.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return result;
        }
        
        /** 
         * JavaBean转换成xml去除xml声明部分 
         * @param obj 
         * @param encoding  
         * @return  
         */
        public static String convertToXmlIgnoreXmlHead(Object obj, String encoding) {
            String result = null;
            try {
                JAXBContext context = JAXBContext.newInstance(obj.getClass());
                Marshaller marshaller = context.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
                marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
                StringWriter writer = new StringWriter();
                marshaller.marshal(obj, writer);
                result = writer.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return result;
        }
        
        
        
    
        /** 
         * xml转换成JavaBean 
         * @param xml 
         * @param c 
         * @return 
         */
        @SuppressWarnings("unchecked")
        public static <T> T converyToJavaBean(String xml, Class<T> c) {
            T t = null;
            try {
                JAXBContext context = JAXBContext.newInstance(c);
                Unmarshaller unmarshaller = context.createUnmarshaller();
                t = (T) unmarshaller.unmarshal(new StringReader(xml));
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return t;
        }
    
    }
  • 相关阅读:
    bzoj 2257 (JSOI 2009) 瓶子与燃料
    bzoj 2257 (JSOI 2009) 瓶子与燃料
    splay 模板 洛谷3369
    费用流 模板 洛谷3381
    bzoj 1024 [SCOI2009]生日快乐——模拟
    bzoj 3231 [Sdoi2008]递归数列——矩阵乘法
    hdu 5823 color II——子集dp(独立集)
    bzoj 1093 [ZJOI2007]最大半连通子图——缩点+拓扑
    洛谷 3959 宝藏——枚举+状压dp
    bzoj 1034 [ZJOI2008]泡泡堂BNB——贪心
  • 原文地址:https://www.cnblogs.com/henuyuxiang/p/14713341.html
Copyright © 2011-2022 走看看