超强大的XML和JavaBean相互转换的工具类

深碍√TFBOYSˉ_ 2022-04-12 02:25 744阅读 0赞
  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.Reader;
  8. import java.io.StringWriter;
  9. import java.text.MessageFormat;
  10. import javax.xml.bind.JAXBContext;
  11. import javax.xml.bind.JAXBException;
  12. import javax.xml.bind.Marshaller;
  13. import javax.xml.bind.Unmarshaller;
  14. import javax.xml.stream.XMLOutputFactory;
  15. import javax.xml.stream.XMLStreamWriter;
  16. /**
  17. * @Description:xml字符串转换Javabean
  18. * @author:lgh
  19. * @date: 2018-11-20 10:55
  20. */
  21. public class JaxbUtils {
  22. @SuppressWarnings("unchecked")
  23. private static <T> T readString(Class<T> clazz, String context)
  24. throws JAXBException {
  25. try {
  26. JAXBContext jc = JAXBContext.newInstance(clazz);
  27. Unmarshaller u = jc.createUnmarshaller();
  28. return (T) u.unmarshal(new File(context));
  29. } catch (JAXBException e) {
  30. throw e;
  31. }
  32. }
  33. @SuppressWarnings("unchecked")
  34. private static <T> T readConfig(Class<T> clazz, String config,
  35. Object... arguments) throws IOException, JAXBException {
  36. InputStream is = null;
  37. try {
  38. if (arguments.length > 0) {
  39. config = MessageFormat.format(config, arguments);
  40. }
  41. JAXBContext jc = JAXBContext.newInstance(clazz);
  42. Unmarshaller u = jc.createUnmarshaller();
  43. is = new FileInputStream(config);
  44. return (T) u.unmarshal(is);
  45. } catch (IOException e) {
  46. throw e;
  47. } catch (JAXBException e) {
  48. throw e;
  49. } finally {
  50. if (is != null) {
  51. is.close();
  52. }
  53. }
  54. }
  55. @SuppressWarnings("unchecked")
  56. private static <T> T readConfigFromStream(Class<T> clazz,
  57. InputStream dataStream) throws JAXBException {
  58. try {
  59. JAXBContext jc = JAXBContext.newInstance(clazz);
  60. Unmarshaller u = jc.createUnmarshaller();
  61. return (T) u.unmarshal(dataStream);
  62. } catch (JAXBException e) {
  63. // logger.trace(e);
  64. throw e;
  65. }
  66. }
  67. @SuppressWarnings("unchecked")
  68. private static <T> T readConfigFromRead(Class<T> clazz, Reader reader)throws JAXBException
  69. {
  70. try {
  71. JAXBContext jc = JAXBContext.newInstance(clazz);
  72. Unmarshaller u = jc.createUnmarshaller();
  73. return (T) u.unmarshal(reader);
  74. } catch (JAXBException e) {
  75. // logger.trace(e);
  76. throw e;
  77. }
  78. }
  79. /**
  80. * xml转换成java bean
  81. * @param xml
  82. * @param clazz
  83. * @return
  84. * @throws JAXBException
  85. */
  86. public static <T> T jaxbConvertXmlToBean(String xml,Class<T> clazz) {
  87. InputStream is = null;
  88. try {
  89. is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
  90. } catch (Exception e) {
  91. e.printStackTrace();
  92. }finally{
  93. if (is != null) {
  94. try {
  95. is.close();
  96. } catch (IOException e) {
  97. e.printStackTrace();
  98. }
  99. }
  100. }
  101. T t = null;
  102. try {
  103. t = JaxbUtils.readConfigFromStream(clazz, is);
  104. } catch (JAXBException e) {
  105. e.printStackTrace();
  106. }
  107. return t;
  108. }
  109. /**
  110. * java bean 转换成xml
  111. * @param object
  112. * @return
  113. * @throws JAXBException
  114. */
  115. public static String jaxbConvertBeanToXml(Object object)throws JAXBException {
  116. StringWriter writer = new StringWriter();
  117. String returnString ="";
  118. try {
  119. JAXBContext jc = JAXBContext.newInstance(object.getClass());
  120. Marshaller m = jc.createMarshaller();
  121. m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  122. m.marshal(object, writer);
  123. returnString=writer.toString();
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. }finally{
  127. if(writer!=null){
  128. try {
  129. writer.close();
  130. } catch (IOException e) {
  131. e.printStackTrace();
  132. }
  133. }
  134. }
  135. return returnString;
  136. }
  137. public static String jaxbBeanToxml(Object object){
  138. try {
  139. JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
  140. Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  141. jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
  142. jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
  143. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  144. XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(baos, (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING));
  145. xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
  146. jaxbMarshaller.marshal(object, xmlStreamWriter);
  147. xmlStreamWriter.writeEndDocument();
  148. xmlStreamWriter.close();
  149. return new String(baos.toByteArray(),"UTF-8");
  150. } catch (Exception e) {
  151. }
  152. return null;
  153. }
  154. public static String jaxbBeanToxmlGBK(Object object){
  155. try {
  156. JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
  157. Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  158. jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
  159. jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
  160. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  161. XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(baos, (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING));
  162. xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
  163. jaxbMarshaller.marshal(object, xmlStreamWriter);
  164. xmlStreamWriter.writeEndDocument();
  165. xmlStreamWriter.close();
  166. return new String(baos.toByteArray(),"GBK");
  167. } catch (Exception e) {
  168. e.printStackTrace();
  169. }
  170. return null;
  171. }
  172. /**
  173. * xml转换成java bean
  174. * @param xml
  175. * @param clazz
  176. * @return
  177. * @throws JAXBException
  178. */
  179. public static <T> T jaxbConvertXmlToBeanByGbk(String xml,Class<T> clazz) {
  180. InputStream is = null;
  181. try {
  182. is = new ByteArrayInputStream(xml.getBytes("GBK"));
  183. } catch (Exception e) {
  184. e.printStackTrace();
  185. }finally{
  186. if (is != null) {
  187. try {
  188. is.close();
  189. } catch (IOException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. }
  194. T t = null;
  195. try {
  196. t = JaxbUtils.readConfigFromStream(clazz, is);
  197. } catch (JAXBException e) {
  198. e.printStackTrace();
  199. }
  200. return t;
  201. }
  202. }

针对上述工具类,给记得给JavaBean加上相应注解如下:

  1. @XmlAccessorType(XmlAccessType.FIELD)
  2. @XmlRootElement(name = "TransType")
  3. @Data
  4. public class TransType {
  5. @XmlElement(name = "BaseInfo",required = true)
  6. protected BaseInfo baseInfo;
  7. @XmlElement(name = "OutputData",required = true)
  8. protected OutputData outputData;

如有披露或问题欢迎留言或者入群探讨

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NDcwNzMz_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,744人围观)

还没有评论,来说两句吧...

相关阅读