以前也用过digester将xml对象转换成javabean,但当时的xml文件是比较标准的格式
类似于很多返回对象被封装到一个集合。
<result>
<resultInfoList>
<resultInfo>
<refNo>123</refNo>
<title>你我</title>
</resultInfo>
<resultInfo>
<refNo>1234</refNo>
<title>你</title>
</resultInfo>
</resultInfoList>
</result>
这样的结构非常好处理,甚至一眼就可以看见javabean该怎么定义
<?xml version="1.0" encoding="UTF-8"?>
<digester-rules>
<object-create-rule pattern="result"
classname="org.javaeye.digester.Result" />
<!-- 对应在文件中的java bean实体类-->
<pattern value="result/resultInfoList/resultInfo">
<object-create-rule classname="org.javaeye.digester.ResultInfo " />
<call-method-rule pattern="Refno" methodname="setRefNo"
paramcount="0" />
<call-method-rule pattern="Title" methodname="setTitle"
paramcount="0" />
<set-next-rule methodname="addResultInfo" />
</pattern>
</digester-rules>
一些有层次感的结构也可以照葫芦画瓢
但,今天碰到了这种结构,根节点直接跟属性,还是纠结了一阵子
<result>
<code>1</code>
<message></message>
<resultList>
<resultInfo>
<title>你好</title>
…
</resultInfo>
</resultList>
<pagination>
<totlePage>20</totlePage>
</pagination>
<result>
我尝试用digister的映射xml来将这个结果集和javabean对应起来,尝试很多遍,都失败了,如果直接用一个Result对象来匹配,code放不进来,最后不得不在外面套了一层
其中:
public class ResultDO implements Serializable{
private static final long serialVersionUID = 855400932557733834L;
private int code;
private String message;
private Pagination pagination;
private List<ResultInfoDO> resultList = new ArrayList< ResultInfoDO >();
public void setPagination(Pagination pagination) {
this.pagination = pagination;
}
public void addResultInfo(ResultInfoDO resultInfoDO){
resultList.add(resultInfoDO);
}
public void setCode(int code) {
this.code = code;
}
public void setMessage(String message) {
this.message = message;
}
...
}
然后把它封装到一个结果类中:
public class FinalResultDO implements Serializable{
private static final long serialVersionUID = -2176535677372216856L;
private ResultDO resultDO;
public ResultDO getResultDO() {
return resultDO;
}
public void setResultDO(ResultDO resultDO) {
this.resultDO = resultDO;
}
...
}
Digester digester =DigesterLoader.createDigester(rules);
digester.push(finalResultDO );
对应的规则rule.xml:
<?xml version="1.0" encoding="GBK"?>
<digester-rules>
<pattern value="result">
<object-create-rule classname="org.javaeye.digester.ResultDO" />
<set-next-rule methodname="setResultDO"/>
<set-properties-rule />
<bean-property-setter-rule pattern="code" />
<bean-property-setter-rule pattern="message" />
</pattern>
<pattern value="result/resultList/resultInfo">
<object-create-rule classname="org.javaeye.digester.resultInfoDO" />
<set-next-rule methodname="addResultInfo"/>
<set-properties-rule />
<bean-property-setter-rule pattern="titile" />
…
</pattern>
<pattern value="result/pagination">
<object-create-rule classname="ogr.javaeye.digester.Pagination" />
<set-next-rule methodname="setPagination"/>
<set-properties-rule />
<bean-property-setter-rule pattern="totalPage" />
</pattern>
</digester-rules>
这种结构很清晰,解析的过程也一目了然,但美中不足就是针对根节点直接跟属性,外面多余了一层封装,相当于把根节点之内的内容当作一个javabean,放到了结果对象中。
我想把ResultDO直接放到Digester容器中进行匹配,但尝试了几种配置的写法,还是失败了,对digester的了解还是太浅了。