我有一个xml doc,看起来像这样:
<response>
<total>1000</total>
<warning>warning1</warning>
<warning>warning2</warning>
</response>
我的对象如下:
public class Response {
private BigDecimal total;
private List<String> warnings=new ArrayList<String>();
public List<String> getWarnings() {
return warnings;
}
public void setWarnings(List<String> warnings) {
this.warnings = warnings;
}
public BigDecimal getTotal() {
return total;
}
public void setTotal(BigDecimal total) {
this.total = total;
}
public void addWarning(String warning) {
warnings.add(warning);
}
}
我正试图像这样映射:
Digester digester = new Digester();
digester.setValidating( false );
digester.addObjectCreate( "response", Response.class );
digester.addBeanPropertySetter( "response/total", "total" );
digester.addObjectCreate("response/warning","warnings", ArrayList.class);
digester.addCallMethod("response/warning", "add", 1);
digester.addCallParam("response/warning", 0);
ret = (Rate)digester.parse(new ByteArrayInputStream(xml.getBytes()));
但是,我不能让它填充列表。总数确实设置正确。对于它的价值,我无法控制XML,但可以更改我的Response对象。有任何想法吗?提前谢谢。
答案 0 :(得分:4)
您的addWarning
课程中已经有Response
方法,warnings
也已初始化。
只需重写你的规则:
Digester digester = new Digester();
digester.setValidating( false );
digester.addObjectCreate("response", Response.class );
digester.addBeanPropertySetter("response/total", "total" );
digester.addCallMethod("response/warning", "addWarning", 1);
digester.addCallParam("response/warning", 0);
就是这样。