xml 转对象,对象转xml

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

首先pom 引入jar

http://mvnrepository.com/artifact/org.simpleframework/simple-xml/2.7.1

  1. <!-- https://mvnrepository.com/artifact/org.simpleframework/simple-xml -->
  2. <dependency>
  3. <groupId>org.simpleframework</groupId>
  4. <artifactId>simple-xml</artifactId>
  5. <version>2.7.1</version>
  6. </dependency>
  1. import org.apache.commons.logging.Log;
  2. import org.apache.commons.logging.LogFactory;
  3. import org.simpleframework.xml.Serializer;
  4. import org.simpleframework.xml.core.Persister;
  5.  
  6. import java.io.ByteArrayInputStream;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.UnsupportedEncodingException;
  9.  
  10. public class XMLPaser
  11. {
  12. public static final Log LOG = LogFactory.getLog(XMLPaser.class);
  13. /**
  14. * 解析xml为JavaBean
  15. *
  16. * @param xmlString xml
  17. * @param type JavaBean
  18. * @return Object JavaBean
  19. * @throws UnsupportedEncodingException
  20. * @see [类、类#方法、类#成员]
  21. */
  22. public static <T> T getDeserializationXml(String xmlString,Class<T> type) throws UnsupportedEncodingException
  23. {
  24. LOG.debug(xmlString);
  25. Object obj = getDeserializationXml(xmlString.getBytes("UTF-8"),type);
  26. return type.cast(obj);
  27. }
  28.  
  29. public static Object getDeserializationXml(byte[] xmlBytes,Class<?> type)
  30. {
  31. Object object = null;
  32. Serializer serializer = new Persister();
  33. try
  34. {
  35. ByteArrayInputStream is = new ByteArrayInputStream(xmlBytes);
  36. object = serializer.read(type,is);
  37. }
  38. catch (Exception e)
  39. {
  40. LOG.error(e);
  41. }
  42.  
  43. return object;
  44. }
  45.  
  46. /**
  47. * 把字符串中的特殊字符用xml指定的字符代替
  48. *
  49. * @param str 需要转换的字符串
  50. * @return 转换后的字符串
  51. */
  52. public static String changeStrToXml(Object str)
  53. {
  54. String tempStr = null == str ? "" : str.toString();
  55. // 转换所有&为&
  56. tempStr = tempStr.replaceAll("[&]","&");
  57. // 转换所有<为<
  58. tempStr = tempStr.replaceAll("[<]","<");
  59. // 转换所有>为>
  60. tempStr = tempStr.replaceAll("[>]",">");
  61. // 转换所有‘为'
  62. tempStr = tempStr.replaceAll("[']","'");
  63. // 转换所有"为"
  64. tempStr = tempStr.replaceAll("[\"]",""");
  65. // 转换所有(为(
  66. tempStr = tempStr.replaceAll("[(]","(");
  67. // 转换所有)为)
  68. tempStr = tempStr.replaceAll("[)]",")");
  69. // 转换所有%为%
  70. tempStr = tempStr.replaceAll("[%]","%");
  71. // 转换所有+为+
  72. tempStr = tempStr.replaceAll("[+]","+");
  73. // 转换所有-为-
  74. tempStr = tempStr.replaceAll("[-]","-");
  75.  
  76. return tempStr;
  77. }
  78.  
  79. /**
  80. * 把xml中的的特殊字符用转换回原始数据
  81. *
  82. * @param xmlStr 需要转换的字符串
  83. * @return 转换后的字符串
  84. */
  85. public static String changeXmlToStr(String xmlStr)
  86. {
  87. if (StringUtil.isNull(xmlStr))
  88. {
  89. return "";
  90. }
  91.  
  92. // 转换所有&为&
  93. xmlStr = xmlStr.replaceAll("&","&");
  94. // 转换所有<为<
  95. xmlStr = xmlStr.replaceAll("<","<");
  96. // 转换所有>为>
  97. xmlStr = xmlStr.replaceAll(">",">");
  98. // 转换所有'为‘
  99. xmlStr = xmlStr.replaceAll("'","'");
  100. // 转换所有""
  101. xmlStr = xmlStr.replaceAll(""","\"");
  102. // 转换所有(为(
  103. xmlStr = xmlStr.replaceAll("(","(");
  104. // 转换所有)为)
  105. xmlStr = xmlStr.replaceAll(")",")");
  106. // 转换所有%为%
  107. xmlStr = xmlStr.replaceAll("%","%");
  108. // 转换所有+为+
  109. xmlStr = xmlStr.replaceAll("+","+");
  110. // 转换所有-为-
  111. xmlStr = xmlStr.replaceAll("-","-");
  112.  
  113. return xmlStr;
  114. }
  115.  
  116. /**
  117. * 用simple将JavaBean生成xml
  118. *
  119. * @param object Object
  120. * @return String
  121. * @see [类、类#方法、类#成员]
  122. */
  123. public static String getSerializationXml(Object object)
  124. {
  125. String xmlString = "";
  126.  
  127. if (null == object)
  128. {
  129. return xmlString;
  130. }
  131.  
  132. try
  133. {
  134. Serializer serializer = new Persister();
  135. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8);
  136.  
  137. serializer.write(object,byteArrayOutputStream);
  138. xmlString = new String(byteArrayOutputStream.toByteArray());
  139. }
  140. catch (Exception e)
  141. {
  142. LOG.error(e);
  143. }
  144.  
  145. return xmlString;
  146. }
  147.  
  148. }

猜你在找的XML相关文章