XStream-bean轉換為xml是,攜帶CDATA
阿新 • • 發佈:2018-12-14
pom依賴:
<dependency>
<groupId>xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.2.2</version>
</dependency>
import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.core.util.QuickWriter; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import com.thoughtworks.xstream.io.xml.XppDriver; import java.io.Writer; public class XStreamUtil { private static String PREFIX_CDATA = "<![CDATA["; private static String SUFFIX_CDATA = "]]>"; /** * 全部轉化 */ public static XStream initXStream() { return new XStream(new XppDriver() { @Override public HierarchicalStreamWriter createWriter(Writer out) { return new PrettyPrintWriter(out) { protected void writeText(QuickWriter writer, String text) { // if (text.startsWith(PREFIX_CDATA) && // text.endsWith(SUFFIX_CDATA)) { writer.write(PREFIX_CDATA + text + SUFFIX_CDATA); // } else { // super.writeText(writer, text); // } } }; } }); } }