本文共 4762 字,大约阅读时间需要 15 分钟。
1、
2、 3、
* CDATA具体数据
compile ('com.thoughtworks.xstream:xstream:1.4.7') { exclude group: 'xmlpull', module: 'xmlpull' }
1、第一层:updatedata是根节点,有子节点table且多个,用list<bean>
2、第二层:table下都只有一个个单独元素,是值的话直接用string xxSameAsElment来获取好了,有节点的话用bean 3、第三层:有N个相同数据,标注子节点,且用List<String>来获取@XStreamAlias("updatedata")//类注解(必须写),根节点public class ZydUpdateDataBean { @XStreamImplicit(itemFieldName = "table")//节点注解(必须写),有N个用List private Listtables; public List getTables() { return tables; } public void setTables(List tables) { this.tables = tables; } //2 : 每个table下面可以看到有3个元素 public static class ZydTableBean{ String name; String field; ZydValues values; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getField() { return field; } public void setField(String field) { this.field = field; } public ZydValues getValues() { return values; } public void setValues(ZydValues values) { this.values = values; } //3 public static class ZydValues{ @XStreamImplicit(itemFieldName = "value")//节点注解(必须写) private List valueList; public List getValueList() { return valueList; } public void setValueList(List valueList) { this.valueList = valueList; } } }}
public class XStreamUtil { public static String PREFIX_CDATA = ""; /** * 全部转化 */ public static XStream initXStream() { return new XStream(new XppDriver() { @Override public HierarchicalStreamWriter createWriter(Writer out) { return new PrettyPrintWriter(out) { protected void writeText(QuickWriter writer, String text) { // if (text.startsWith(PREFIX_CDATA) && // text.endsWith(SUFFIX_CDATA)) { writer.write(PREFIX_CDATA + text + SUFFIX_CDATA); // } else { // super.writeText(writer, text); // } } }; } }); } /** * 初始化XStream可支持某一字段可以加入CDATA标签,如果需要某一字段使用原文,就需要在String类型的text的头加上 * ""标签, 以供XStream输出时进行识别 * @param isAddCDATA 是否支持CDATA标签 */ public static XStream initXStream(boolean isAddCDATA) { XStream xstream = null; if (isAddCDATA) { xstream = new XStream(new XppDriver() { @Override public HierarchicalStreamWriter createWriter(Writer out) { return new PrettyPrintWriter(out) { protected void writeText(QuickWriter writer, String text) { if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) { writer.write(text); } else { super.writeText(writer, text); } } }; } }); } else { xstream = new XStream(); } return xstream; }}
//去掉乱七八糟数据updateData = updateData.replace("-200","");//XStream解析器:xml->beanboolean flag = updateData.contains(XStreamUtil.PREFIX_CDATA);XStream xStream = XStreamUtil.initXStream(flag);xStream.processAnnotations(ZydUpdateDataBean.class);ZydUpdateDataBean dataBean = (ZydUpdateDataBean) xStream.fromXML(updateData);
转载地址:http://kfjko.baihongyu.com/