用 Jaxb 对 XML 和 JavaBean相互转换

前端之家收集整理的这篇文章主要介绍了用 Jaxb 对 XML 和 JavaBean相互转换前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

用ConcurrentMap 对JAXBContext进行缓存,提高效率。


  1. private static ConcurrentMap<Class<?>,JAXBContext> jaxbContexts = new ConcurrentHashMap<Class<?>,JAXBContext>();
  2.  
  3. /**
  4. * 把xml转换成对应的bean
  5. *
  6. * @param <T>
  7. * @param clazz
  8. * @param xml
  9. * @return
  10. */
  11. public static <T> T parseXml2Bean(Class<T> clazz,String xml) {
  12. if (StringUtils.isBlank(xml)) {
  13. return null;
  14. }
  15. try {
  16. StringReader reader = new StringReader(xml);
  17. return clazz.cast(createUnmarshaller(clazz).unmarshal(reader));
  18. } catch (JAXBException e) {
  19. throw new RuntimeException("Could not parse xml to class [" + clazz + "]: " + e.getMessage(),e);
  20. }
  21. }
  22.  
  23. /**
  24. * 把bean转换成对应的xml
  25. *
  26. * @param obj
  27. * @return
  28. */
  29. public static String parseBean2Xml(Object obj) {
  30. return parseBean2Xml(obj,null,false);
  31. }
  32.  
  33. /**
  34. * 是否省略xml头信息
  35. *
  36. * @param obj
  37. * @param encoding
  38. * 编码格式
  39. * @param bool
  40. * 是否省略xml头信息
  41. * @return
  42. */
  43. public static String parseBean2Xml(Object obj,String encoding,boolean bool) {
  44. if (obj == null) {
  45. return null;
  46. }
  47. try {
  48. ByteArrayOutputStream os = new ByteArrayOutputStream();
  49. createMarshaller(obj.getClass(),encoding,bool).marshal(obj,os);
  50. return os.toString("UTF-8");
  51. } catch (JAXBException e) {
  52. throw new RuntimeException("Could not parse [" + obj + "] to xml " + e.getMessage(),e);
  53. } catch (UnsupportedEncodingException e) {
  54. throw new RuntimeException(e.getMessage(),e);
  55. }
  56. }
  57.  
  58. /**
  59. * 创建Marshaller并设定encoding(可为null). <br/>
  60. * 线程不安全,需要每次创建或pooling。
  61. */
  62. private static Marshaller createMarshaller(Class<?> clazz,boolean bool) {
  63. try {
  64. JAXBContext jaxbContext = getJaxbContext(clazz);
  65. Marshaller marshaller = jaxbContext.createMarshaller();
  66. marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);// 是否格式化生成的xml串
  67. marshaller.setProperty(Marshaller.JAXB_FRAGMENT,bool);// 是否省略xml头信息
  68. if (StringUtils.isNotBlank(encoding)) {
  69. marshaller.setProperty(Marshaller.JAXB_ENCODING,encoding);
  70. }
  71. return marshaller;
  72. } catch (JAXBException e) {
  73. throw new RuntimeException("Could not createMarshaller for class [" + clazz + "]: " + e.getMessage(),e);
  74. }
  75. }
  76.  
  77. /**
  78. * 创建UnMarshaller. <br/>
  79. * 线程不安全,需要每次创建或pooling。
  80. */
  81. private static Unmarshaller createUnmarshaller(Class<?> clazz) {
  82. try {
  83. JAXBContext jaxbContext = getJaxbContext(clazz);
  84. return jaxbContext.createUnmarshaller();
  85. } catch (JAXBException e) {
  86. throw new RuntimeException("Could not createUnmarshaller for class [" + clazz + "]: " + e.getMessage(),e);
  87. }
  88. }
  89.  
  90. /**
  91. * 维护jaxbContexts
  92. */
  93. private static JAXBContext getJaxbContext(Class<?> clazz) {
  94. Validate.notNull(clazz,"'clazz' must not be null");
  95. JAXBContext jaxbContext = jaxbContexts.get(clazz);
  96. try {
  97. if (jaxbContext == null) {
  98. jaxbContext = JAXBContext.newInstance(clazz);
  99. JAXBContext j = jaxbContexts.putIfAbsent(clazz,jaxbContext);
  100. if (j != null) {
  101. jaxbContext = j;
  102. }
  103. }
  104. } catch (JAXBException e) {
  105. throw new RuntimeException(
  106. "Could not instantiate JAXBContext for class [" + clazz + "]: " + e.getMessage(),e);
  107. }
  108. return jaxbContext;
  109. }

猜你在找的XML相关文章