XStream二重下線問題の処理について
1674 ワード
数回のデバッグを経て、やっと有効な結果が得られました.方法は以下の通りです.
使用方法は次のとおりです.
public class XStreamFactory {
public static XStream getXStream() {
final NameCoder nameCoder = new NoNameCoder();
XStream xStream = new XStream(new XppDomDriver(nameCoder) {
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
// xml
//return new PrettyPrintWriter(out, nameCoder) {
// xml ,
return new CompactWriter(out, nameCoder) {
boolean cdata = false;
@Override
public void startNode(String name, Class clazz) {
super.startNode(name, clazz);
// CDATA
cdata = "String".equals(clazz.getSimpleName());
}
@Override
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else {
writer.write(text);
}
}
};
}
});
return xStream;
}
}
使用方法は次のとおりです.
XStream xStream = XStreamFactory.getStream();
// xml to java object
xStream.ignoreUnknownElements();
xStream.alias("xml", Class< java object>);
xStream.fromXML("<xml><tag>aaa</tag></xml>");
// java object to xml
xStream.toXML(java object);