參考
1匆浙、Android 用 XStream 將復(fù)雜xml解析為javabean
2安寺、XStream支持CDATA標(biāo)簽
3、Retrofit 用Soap協(xié)議訪問WebService 詳解
實(shí)例
留著上一篇retrofit訪問webservice后首尼,最后到訪問成功挑庶,到需要解析CDATA數(shù)據(jù)為止,繼續(xù)下面的工作软能,先看下數(shù)據(jù)格式吧:
* CDATA具體數(shù)據(jù)
<![CDATA[
<updatedata>
<table>
<name>table_xxx</name>
<field>id, codeid, name, pid, remark, inputdate, modifydate, status, type_num</field>
<values>
<value>302|302|亞硝酸鹽|1|檢測項(xiàng)目|2016-08-24 10:58:51.0|null|C|null</value>
...
<value>472|472|醬油氨基態(tài)氮|1|檢測項(xiàng)目|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 應(yīng)該也可以)
1跋核、app 下的build.gradle岖瑰,添加依賴
compile ('com.thoughtworks.xstream:xstream:1.4.7') {
exclude group: 'xmlpull', module: 'xmlpull'
}
2、xml的bean建立
2.1砂代、@XStreamAlias 根節(jié)點(diǎn)
2.2蹋订、@XStreamImplicit 子節(jié)點(diǎn)
2.3、解釋:(1)泊藕、子節(jié)點(diǎn)是值的話直接用 String xxSameAsElementName 來獲取 (2)辅辩、子節(jié)點(diǎn)里面還有子節(jié)點(diǎn)的話用bean來獲取,名字可取Element一樣的娃圆,不一樣要注解 (3)搏熄、子節(jié)點(diǎn)有N個相同的話用List<Bean>來獲取尾菇,且需要注解子節(jié)點(diǎn)
1没佑、第一層:updatedata是根節(jié)點(diǎn)唇撬,有子節(jié)點(diǎn)table且多個按脚,用list<bean>
2昔榴、第二層:table下都只有一個個單獨(dú)元素绽族,是值的話直接用string xxSameAsElment來獲取好了卖怜,有節(jié)點(diǎn)的話用bean
3、第三層:有N個相同數(shù)據(jù)础爬,標(biāo)注子節(jié)點(diǎn)甫贯,且用List<String>來獲取
@XStreamAlias("updatedata")//類注解(必須寫),根節(jié)點(diǎn)
public class ZydUpdateDataBean {
@XStreamImplicit(itemFieldName = "table")//節(jié)點(diǎn)注解(必須寫)看蚜,有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")//節(jié)點(diǎn)注解(必須寫)
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 = "]]>";
/**
* 全部轉(zhuǎn)化
*/
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標(biāo)簽,如果需要某一字段使用原文,就需要在String類型的text的頭加上
* "<![CDATA["和結(jié)尾處加上"]]>"標(biāo)簽, 以供XStream輸出時進(jìn)行識別
* @param isAddCDATA 是否支持CDATA標(biāo)簽
*/
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供炎、具體解析
//去掉亂七八糟數(shù)據(jù)
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);