现在我使用xStream
将对象解析为xml格式的String。我希望当我解析xml时,我可以根据不同的文件做我想做的事情,比如如果我遇到提交的A,我会在它之前添加CDATA Block,当我遇到提交的B时,我会把它变成大写,像这样的东西,这就是我现在正在做的事情。
public static String textMessageToXml(ResponseTextMessage textMsg){
xstream.alias("xml", textMsg.getClass());
return xstream.toXML(textMsg);
}
private static XStream xstream = new XStream(new XppDriver() {
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out) {
// Add CDATA block
boolean cdata = ...;//I need to add cdata for some fields only
boolean isNeedUpper=...;//only some will need this
protected void writeText(QuickWriter writer, String text) {
if (cdata) {
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
} else if (isNeedUpper){
writer.write(text.toUpperCase());
}
else writer.write(text);
}
};
}
});