xstream 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, Class clazz) {
                                    super.startNode(name, clazz);
                                    cdata = name.equalsIgnoreCase("value");
                                }

                                @Override
                                public void setValue(String text) {
                                    super.setValue(text);    //To change body of overridden methods use File | Settings | File Templates.
                                }

                                @Override
                                protected void writeText(QuickWriter writer, String text) {
                                    if(cdata){
                                        writer.write("<![CDATA[");
                                        writer.write(text);
                                        writer.write("]]>");
                                    }else {
                                        writer.write(text);
                                    }
                                }
                            };
                        }
                    }
            );
xStream.alias("parameters", List.class);
xStream.alias("parameter", Parameter.class);
xStream.useAttributeFor(Parameter.class,"index");
xStream.useAttributeFor(Parameter.class,"label");
xStream.useAttributeFor(Parameter.class,"paramType");
xStream.useAttributeFor(Parameter.class,"required");
xStream.useAttributeFor(Parameter.class,"editable");
xStream.useAttributeFor(Parameter.class,"encrypt");
return xStream.toXML(obj);
重点在于 boolean 变量的修改

你可能感兴趣的:(xstream,CDATA)