SmartGWT DataSource字符串XML

时间:2012-04-30 16:07:15

标签: java xml datasource smartgwt

我目前正在使用SmartGWT网格,该网格使用DataSource对象,该对象接收xml文件。一切都很好用这种方法,但我想知道我是否可以发送一个带有xml结构的字符串。如下所示:

String xml = "<listgrid><data><campo1></campo1>hola<campo2>mundo</campo2></data></listgrid>";
setData(xml);

这是伪代码,但它应该给读者一个想法。

我搜索过,发现没有符合我要求的例子。

1 个答案:

答案 0 :(得分:2)

有一种更好的方法。你想要做的是动态填充dataSource。

以下是一个例子:

public void onModuleLoad() {
    DataSourceTextField continentField = new DataSourceTextField("continent");
    continentField.setPrimaryKey(true);

    DataSource dataSource = new DataSource();
    dataSource.setClientOnly(true);
    dataSource.setFields(continentField);
    for (CountryRecord record : new CountryData().getNewRecords()) {
        dataSource.addData(record);
    }

    ListGrid myGrid = new ListGrid();
    myGrid.setWidth(200);
    myGrid.setHeight(100);
    myGrid.setDataSource(dataSource);
    myGrid.fetchData();
    myGrid.draw();
}

class CountryData {

    public CountryRecord[] getNewRecords() {
        return new CountryRecord[] { 
                new CountryRecord("North America"), 
                new CountryRecord("Asia") };
    }
}

class CountryRecord extends ListGridRecord {
    public CountryRecord(String continent) {
        setContinent(continent);
    }

    public void setContinent(String continent) {
        setAttribute("continent", continent);
    }

    public String getContinent() {
        return getAttributeAsString("continent");
    }
}
相关问题