JaxbUtil.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.happy.Until.weixin;
  2. import org.apache.commons.lang.StringUtils;
  3. import javax.xml.bind.*;
  4. import javax.xml.bind.annotation.XmlAnyElement;
  5. import javax.xml.namespace.QName;
  6. import java.io.StringReader;
  7. import java.io.StringWriter;
  8. import java.util.Collection;
  9. import java.util.Iterator;
  10. import java.util.Map;
  11. import java.util.Map.Entry;
  12. import java.util.Set;
  13. /**
  14. * 使用Jaxb2.0实现XML<->Java Object的Binder.
  15. *
  16. * 特别支持Root对象是List的情形.
  17. *
  18. * @author
  19. */
  20. public class JaxbUtil {
  21. // 多线程安全的Context.
  22. private JAXBContext jaxbContext;
  23. /**
  24. * @param types
  25. * 所有需要序列化的Root对象的类型.
  26. */
  27. public JaxbUtil(Class<?>... types) {
  28. try {
  29. jaxbContext = JAXBContext.newInstance(types);
  30. } catch (JAXBException e) {
  31. throw new RuntimeException(e);
  32. }
  33. }
  34. /**
  35. * Java Object->Xml.
  36. */
  37. public String toXml(Object root, String encoding) {
  38. try {
  39. StringWriter writer = new StringWriter();
  40. createMarshaller(encoding).marshal(root, writer);
  41. return writer.toString();
  42. } catch (JAXBException e) {
  43. throw new RuntimeException(e);
  44. }
  45. }
  46. /**
  47. * Java Object->Xml, 特别支持对Root Element是Collection的情形.
  48. */
  49. @SuppressWarnings("unchecked")
  50. public String toXml(Collection root, String rootName, String encoding) {
  51. try {
  52. CollectionWrapper wrapper = new CollectionWrapper();
  53. wrapper.collection = root;
  54. JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(
  55. new QName(rootName), CollectionWrapper.class, wrapper);
  56. StringWriter writer = new StringWriter();
  57. createMarshaller(encoding).marshal(wrapperElement, writer);
  58. return writer.toString();
  59. } catch (JAXBException e) {
  60. throw new RuntimeException(e);
  61. }
  62. }
  63. /**
  64. * Xml->Java Object.
  65. */
  66. @SuppressWarnings("unchecked")
  67. public <T> T fromXml(String xml) {
  68. try {
  69. StringReader reader = new StringReader(xml);
  70. return (T) createUnmarshaller().unmarshal(reader);
  71. } catch (JAXBException e) {
  72. throw new RuntimeException(e);
  73. }
  74. }
  75. /**
  76. * Xml->Java Object, 支持大小写敏感或不敏感.
  77. */
  78. @SuppressWarnings("unchecked")
  79. public <T> T fromXml(String xml, boolean caseSensitive) {
  80. try {
  81. String fromXml = xml;
  82. if (!caseSensitive)
  83. fromXml = xml.toLowerCase();
  84. StringReader reader = new StringReader(fromXml);
  85. return (T) createUnmarshaller().unmarshal(reader);
  86. } catch (JAXBException e) {
  87. throw new RuntimeException(e);
  88. }
  89. }
  90. /**
  91. * 创建Marshaller, 设定encoding(可为Null).
  92. */
  93. public Marshaller createMarshaller(String encoding) {
  94. try {
  95. Marshaller marshaller = jaxbContext.createMarshaller();
  96. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
  97. if (StringUtils.isNotBlank(encoding)) {
  98. marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
  99. }
  100. return marshaller;
  101. } catch (JAXBException e) {
  102. throw new RuntimeException(e);
  103. }
  104. }
  105. /**
  106. * 创建UnMarshaller.
  107. */
  108. public Unmarshaller createUnmarshaller() {
  109. try {
  110. return jaxbContext.createUnmarshaller();
  111. } catch (JAXBException e) {
  112. throw new RuntimeException(e);
  113. }
  114. }
  115. /**
  116. * 封装Root Element 是 Collection的情况.
  117. */
  118. public static class CollectionWrapper {
  119. @SuppressWarnings("unchecked")
  120. @XmlAnyElement
  121. protected Collection collection;
  122. }
  123. /**
  124. * 把map转换成xml格式
  125. * @param params
  126. * @return
  127. * @throws Exception
  128. */
  129. public static String getRequestXml(Map<String, String> params) throws Exception{
  130. StringBuffer sb = new StringBuffer();
  131. sb.append("<xml>");
  132. Set<Entry<String, String>> es = params.entrySet();
  133. Iterator<Entry<String, String>> it = es.iterator();
  134. while (it.hasNext()) {
  135. Entry<String, String> entry = (Entry<String, String>) it
  136. .next();
  137. String k = (String) entry.getKey();
  138. String v = (String) entry.getValue();
  139. if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k)) {
  140. sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
  141. } else {
  142. sb.append("<" + k + ">" + v + "</" + k + ">");
  143. }
  144. }
  145. sb.append("</xml>");
  146. return new String(sb.toString().toString().getBytes(), "utf-8");
  147. }
  148. }