DtoXmlUtils.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. *
  3. * Copyright (c) behosoft Co.,Ltd.
  4. * All Rights Reserved.
  5. *
  6. * This software is the confidential and proprietary information of behosoft.
  7. * (Social Security Department). You shall not disclose such
  8. * Confidential Information and shall use it only in accordance with
  9. * the terms of the license agreement you entered into with behosoft.
  10. *
  11. * Distributable under GNU LGPL license by gnu.org
  12. */
  13. package com.behosoft.util;
  14. import java.io.IOException;
  15. import java.io.StringReader;
  16. import java.io.StringWriter;
  17. import java.util.Date;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. import javax.xml.bind.JAXBContext;
  21. import javax.xml.bind.Marshaller;
  22. import javax.xml.bind.Unmarshaller;
  23. import org.apache.commons.logging.Log;
  24. import org.apache.commons.logging.LogFactory;
  25. /**
  26. * <p>
  27. * Title: 百弘电商物流标准版_[]_[模块名]
  28. * </p>
  29. * <p>
  30. * Description: [描述该类概要功能介绍]
  31. * </p>
  32. *
  33. * @author jiangyonghua
  34. * @version 1.0 2013-10-17
  35. * @author (lastest modification by )
  36. * @since 1.0
  37. */
  38. public class DtoXmlUtils {
  39. public final static Log log = LogFactory.getLog(DtoXmlUtils.class);
  40. public static Map<String, Marshaller> marshallerMap = new HashMap<String, Marshaller>();
  41. public static Map<String, Unmarshaller> UnmarshallerMap = new HashMap<String, Unmarshaller>();
  42. public static synchronized String DtoToXml(Object dtoObject) throws Exception {
  43. String xml = "";
  44. StringWriter stringWriter = new StringWriter();
  45. try {
  46. String className = dtoObject.getClass().getName();
  47. Marshaller marshaller_res = null;
  48. if (marshallerMap.containsKey(className)) {
  49. marshaller_res = marshallerMap.get(className);
  50. }
  51. else {
  52. marshaller_res = JAXBContext.newInstance(dtoObject.getClass()).createMarshaller();
  53. marshallerMap.put(className, marshaller_res);
  54. }
  55. marshaller_res.marshal(dtoObject, stringWriter);
  56. xml = stringWriter.toString();
  57. stringWriter.flush();
  58. }
  59. catch (Exception ex) {
  60. System.out.println("toxml" + ex.toString());
  61. throw ex;
  62. }
  63. finally {
  64. try {
  65. if (stringWriter != null)
  66. stringWriter.close();
  67. }
  68. catch (IOException e) {
  69. e.printStackTrace();
  70. }
  71. }
  72. return xml;
  73. }
  74. public static synchronized Object xmlToDto(String xml, Class classType) throws Exception {
  75. Object obj = null;
  76. try {
  77. Unmarshaller unmarshaller_req = null;
  78. String className = classType.getName();
  79. if (UnmarshallerMap.containsKey(className)) {
  80. unmarshaller_req = UnmarshallerMap.get(className);
  81. }
  82. else {
  83. unmarshaller_req = JAXBContext.newInstance(classType).createUnmarshaller();
  84. UnmarshallerMap.put(className, unmarshaller_req);
  85. }
  86. obj = unmarshaller_req.unmarshal(new StringReader(xml));
  87. }
  88. catch (Exception ex) {
  89. System.out.println("todto" + ex.toString());
  90. throw ex;
  91. }
  92. return obj;
  93. }
  94. }