首先pom 引入jar
http://mvnrepository.com/artifact/org.simpleframework/simple-xml/2.7.1
<!-- https://mvnrepository.com/artifact/org.simpleframework/simple-xml --> <dependency> <groupId>org.simpleframework</groupId> <artifactId>simple-xml</artifactId> <version>2.7.1</version> </dependency>
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.simpleframework.xml.Serializer; import org.simpleframework.xml.core.Persister; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; public class XMLPaser { public static final Log LOG = LogFactory.getLog(XMLPaser.class); /** * 解析xml为JavaBean * * @param xmlString xml * @param type JavaBean * @return Object JavaBean * @throws UnsupportedEncodingException * @see [类、类#方法、类#成员] */ public static <T> T getDeserializationXml(String xmlString,Class<T> type) throws UnsupportedEncodingException { LOG.debug(xmlString); Object obj = getDeserializationXml(xmlString.getBytes("UTF-8"),type); return type.cast(obj); } public static Object getDeserializationXml(byte[] xmlBytes,Class<?> type) { Object object = null; Serializer serializer = new Persister(); try { ByteArrayInputStream is = new ByteArrayInputStream(xmlBytes); object = serializer.read(type,is); } catch (Exception e) { LOG.error(e); } return object; } /** * 把字符串中的特殊字符用xml指定的字符代替 * * @param str 需要转换的字符串 * @return 转换后的字符串 */ public static String changeStrToXml(Object str) { String tempStr = null == str ? "" : str.toString(); // 转换所有&为& tempStr = tempStr.replaceAll("[&]","&"); // 转换所有<为< tempStr = tempStr.replaceAll("[<]","<"); // 转换所有>为> tempStr = tempStr.replaceAll("[>]",">"); // 转换所有‘为' tempStr = tempStr.replaceAll("[']","'"); // 转换所有"为" tempStr = tempStr.replaceAll("[\"]","""); // 转换所有(为( tempStr = tempStr.replaceAll("[(]","("); // 转换所有)为) tempStr = tempStr.replaceAll("[)]",")"); // 转换所有%为% tempStr = tempStr.replaceAll("[%]","%"); // 转换所有+为+ tempStr = tempStr.replaceAll("[+]","+"); // 转换所有-为- tempStr = tempStr.replaceAll("[-]","-"); return tempStr; } /** * 把xml中的的特殊字符用转换回原始数据 * * @param xmlStr 需要转换的字符串 * @return 转换后的字符串 */ public static String changeXmlToStr(String xmlStr) { if (StringUtil.isNull(xmlStr)) { return ""; } // 转换所有&为& xmlStr = xmlStr.replaceAll("&","&"); // 转换所有<为< xmlStr = xmlStr.replaceAll("<","<"); // 转换所有>为> xmlStr = xmlStr.replaceAll(">",">"); // 转换所有'为‘ xmlStr = xmlStr.replaceAll("'","'"); // 转换所有"为" xmlStr = xmlStr.replaceAll(""","\""); // 转换所有(为( xmlStr = xmlStr.replaceAll("(","("); // 转换所有)为) xmlStr = xmlStr.replaceAll(")",")"); // 转换所有%为% xmlStr = xmlStr.replaceAll("%","%"); // 转换所有+为+ xmlStr = xmlStr.replaceAll("+","+"); // 转换所有-为- xmlStr = xmlStr.replaceAll("-","-"); return xmlStr; } /** * 用simple将JavaBean生成xml * * @param object Object * @return String * @see [类、类#方法、类#成员] */ public static String getSerializationXml(Object object) { String xmlString = ""; if (null == object) { return xmlString; } try { Serializer serializer = new Persister(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(8); serializer.write(object,byteArrayOutputStream); xmlString = new String(byteArrayOutputStream.toByteArray()); } catch (Exception e) { LOG.error(e); } return xmlString; } }