郑永安
2023-06-19 59e91a4e9ddaf23cebb12993c774aa899ab22d16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.gk.firework.Domain.Utils;
 
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * Created by Administrator on 2017/11/29.
 */
public class JaxbUtils <T>{
    public static String JaxbObj2Xml (Object object,Boolean withhead){
        String xmlString="";
        try {
            StringWriter sw = new StringWriter();
            Marshaller jaxbMarshaller = null;
            try {
               JAXBContext jaxbContext = getContext(object.getClass());
                jaxbMarshaller = jaxbContext.createMarshaller();
            } catch (JAXBException e) {
                e.printStackTrace();
            }
 
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false); // 格式化输出
            jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// 编码格式,默认为utf-8
            jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);// 是否省略xml头信息
 
            jaxbMarshaller.marshal(object, sw);
            xmlString=sw.toString();
            if(withhead)
            {
                xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + xmlString;
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return  xmlString;
    }
 
    public static <T> T JaxbXml2Obj(String xmlstr, Class<T> classz) throws JAXBException {
        T req;
        try {
            JAXBContext context = JAXBContext.newInstance(classz);
            Unmarshaller shaller = context.createUnmarshaller();
 
            req = (T) shaller.unmarshal(new StringReader(xmlstr));
        } catch (JAXBException e) {
            throw e;
        }
        return  req;
    }
 
    public static Map<Class<?>,JAXBContext> contextMap = new ConcurrentHashMap<Class<?>,JAXBContext>();
 
    public static String convertToXml(Object obj) throws JAXBException{
        Class<?> clazz;
        if (obj instanceof JAXBElement){
            clazz = ((JAXBElement<?>)obj).getDeclaredType();
        }else {
            clazz = obj.getClass();
        }
        StringWriter sw = new StringWriter();
        getContext(clazz).createMarshaller().marshal(obj,sw);
        return sw.toString();
    }
 
    public static <T> T convertToJavaBean(String xml,Class<T>clazz)throws JAXBException{
        StringReader reader = new StringReader(xml);
        return getContext(clazz).createUnmarshaller().unmarshal(new StreamSource(reader),clazz).getValue();
    }
 
    private static JAXBContext getContext(Class<?>clazz) throws JAXBException{
        JAXBContext context = contextMap.get(clazz);
        if (null == context){
            context = JAXBContext.newInstance(clazz);
            contextMap.put(clazz,context);
        }
        return context;
    }
 
 
 
 
 
 
 
 
 
 
}