package com.qikemi.packages.xmlJsonAndJavaBean.xstream;
import java.io.Writer;
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;
/**
* CDATA支持
*
* Create Date: 2014年10月13日 下午8:50:11
* Author XieXianbin<a.b@hotmail.com>
* Source Repositories Address: <https://github.com/qikemi/open-wechat-sdk>
*/
public class XstreamUtil {
/**
* 扩展xstream,使其支持CDATA
*/
private static XStream xstream = new XStream(new XppDriver() {
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// 对所有xml节点的转换都增加CDATA标记
boolean cdata = true;
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
public static XStream getXStream() {
xstream.autodetectAnnotations(true);
return xstream;
}
}