Android技术知识Android开发经验谈Android开发

Android XStream 解析xml数据变成bean,支

2018-05-31  本文已影响13人  Kandy_JS

参考

1、Android 用 XStream 将复杂xml解析为javabean
2、XStream支持CDATA标签
3、Retrofit 用Soap协议访问WebService 详解

实例

留着上一篇retrofit访问webservice后,最后到访问成功,到需要解析CDATA数据为止,继续下面的工作,先看下数据格式吧:

 * CDATA具体数据
 <![CDATA[
 <updatedata>
    <table>
        <name>table_xxx</name>
        <field>id, codeid, name, pid, remark, inputdate, modifydate, status, type_num</field>
        <values>
            <value>302|302|亚硝酸盐|1|检测项目|2016-08-24 10:58:51.0|null|C|null</value>
            ...
            <value>472|472|酱油氨基态氮|1|检测项目|2016-08-24 10:58:51.0|null|C|null</value>
        </values>
    </table>-200

    <table>
        <name>table_yyy</name>
        <field>id, inputdate, modifydate, decision_basis, max_limit, min_limit, test_basis, unit, food_type, test_item</field>
        <values>
            ....
        </values>
    </table>
 </updatedata>
 ]]>

好吧。。。还需需要利器来解析,XStream来也(其他 SAX-xml,JSoup-html 应该也可以)

1、app 下的build.gradle,添加依赖
    compile ('com.thoughtworks.xstream:xstream:1.4.7') {
        exclude group: 'xmlpull', module: 'xmlpull'
    }
2、xml的bean建立
2.1、@XStreamAlias 根节点
2.2、@XStreamImplicit 子节点
2.3、解释:(1)、子节点是值的话直接用 String xxSameAsElementName 来获取 (2)、子节点里面还有子节点的话用bean来获取,名字可取Element一样的,不一样要注解 (3)、子节点有N个相同的话用List<Bean>来获取,且需要注解子节点

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 List<ZydTableBean> tables;

    public List<ZydTableBean> getTables() {
        return tables;
    }

    public void setTables(List<ZydTableBean> 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<String> valueList;

            public List<String> getValueList() {
                return valueList;
            }

            public void setValueList(List<String> valueList) {
                this.valueList = valueList;
            }
        }
    }
}
3、支持CDATA的XStream工具编写~~直接Copy来
public class XStreamUtil {
    public static String PREFIX_CDATA = "<![CDATA[";
    public static String SUFFIX_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的头加上
     * "<![CDATA["和结尾处加上"]]>"标签, 以供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;
    }
}
4、具体解析
//去掉乱七八糟数据
updateData = updateData.replace("</table>-200","</table>");
//XStream解析器:xml->bean
boolean flag = updateData.contains(XStreamUtil.PREFIX_CDATA);
XStream xStream = XStreamUtil.initXStream(flag);
xStream.processAnnotations(ZydUpdateDataBean.class);
ZydUpdateDataBean dataBean = (ZydUpdateDataBean) xStream.fromXML(updateData);
上一篇下一篇

猜你喜欢

热点阅读