2011-11-16 138 views
2

沼氣池規則我有一個XML文檔,看起來是這樣的:一個字符串列表

<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對象。有任何想法嗎?提前致謝。

回答

4

您在Response課程中已經有addWarning方法,並且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); 

而就是這樣。

+0

謝謝,這樣做! –

相關問題