|
|
|
@ -9,6 +9,8 @@ import com.thoughtworks.xstream.io.naming.NameCoder;
|
|
|
|
|
import com.thoughtworks.xstream.io.naming.NoNameCoder;
|
|
|
|
|
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
|
|
|
|
|
import com.thoughtworks.xstream.io.xml.XppDomDriver;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.io.Writer;
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
@ -23,8 +25,13 @@ import java.lang.reflect.Field;
|
|
|
|
|
**/
|
|
|
|
|
public class XStreamFactory {
|
|
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(XStreamFactory.class);
|
|
|
|
|
|
|
|
|
|
private static final XStream xStream = XStreamFactory.getXStream();
|
|
|
|
|
|
|
|
|
|
/* 转换重试次数 */
|
|
|
|
|
private static final int RETRY_NUM = 3;
|
|
|
|
|
|
|
|
|
|
public static final String CDATA_PREFIX = "<![CDATA[";
|
|
|
|
|
public static final String CDATA_SUFFIX = "]]>";
|
|
|
|
|
|
|
|
|
@ -120,7 +127,14 @@ public class XStreamFactory {
|
|
|
|
|
public static <T> String toXml(T t) {
|
|
|
|
|
xStream.processAnnotations(t.getClass());
|
|
|
|
|
String headLine = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
|
|
|
|
|
return headLine + xStream.toXML(t);
|
|
|
|
|
for (int i = 1; i < RETRY_NUM; i++) {
|
|
|
|
|
try {
|
|
|
|
|
return headLine + xStream.toXML(t);
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
LOGGER.error("Bean To Xml Error Message:{} Number:{}",e.getMessage(),i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|