我已经搜索并获得了为Axstream添加CDATA标签的解决方案,并且它在我的机器中的Eclipse中运行良好。但是,在我将代码部署到tomcat服务器之后,它始终在toXML(t,writer)或toXML(t)上失败并且IOException,我还尝试将编写器设置为" UTF-8&#34 ;,但仍然没有运气。
处理CDATA的代码是:
XStream xstream = new XStream( new XppDriver(){
@Override
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out){
boolean cdata = false;
@Override
public void startNode(String name) {
super.startNode(name);
cdata = name.equalsIgnoreCase("imagestream");
}
@Override
protected void writeText(QuickWriter writer, String text) {
if(cdata && !text.isEmpty()){
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
}else {
writer.write(text);
}
}
};
}
});
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, Charset.forName("UTF-8")));
xstream.toXML(t,writer);
xml = outputStream.toString("UTF-8");