自分で作ったり提供したりするものは、まず自分自身で使ってみろろということです。自分じゃ使わないものなら人はいくらでも無責任にも無思考にもなれる。そういう投げやりな「サービス」やら「プロダクツ」なんて、だれだってイヤだ。自分が作り手と同時に利用者の立場になれば、ちゃんと使えるレベルのものを提供しようとします。

2010年4月28日水曜日

SmartGWT XML解析と表示

SmartGWTでXMLデータを取得して表示するサンプルを紹介します。

このサンプルもDataSourceとListGridを利用して説明します。


ソースコード:

test.xml
<contacts>
    <contact>
        <name>Bob Smith</name>
        <email>bob@bobco.com</email>
        <organization>Bob Co.</organization>
        <phone>555-1212</phone>
        <address>
            <street>55 bob lane</street>
            <city>New York</city>
            <state>NY</state>
            <zip>10021</zip>
        </address>
    </contact>
    <contact>
        <name>Emily Smith</name>
        <email>emily@bobco.com</email>
        <organization>Bob Co.</organization>
        <phone>555-1212</phone>
        <address>
            <street>55 bob lane</street>
            <city>New York</city>
            <state>NY</state>
            <zip>10021</zip>
        </address>
    </contact>
    <contact>
        <name>Jim Smith</name>
        <email>jim@acmerockets.com</email>
        <organization>Acme rockets</organization>
        <phone>655-2345</phone>
        <address>
            <street>66 New Montgomery St.</street>
            <city>San Francisco</city>
            <state>CA</state>
            <zip>94105</zip>
        </address>
    </contact>
</contacts>

javaコード
DataSource dataSource = new DataSource();
dataSource.setDataFormat(DSDataFormat.XML);
dataSource.setRecordXPath("//contact");
dataSource.setDataURL("/test.xml");

DataSourceTextField nameField = new DataSourceTextField("name", "Name");
DataSourceTextField emailField = new DataSourceTextField("email", "Email");
DataSourceTextField orgField = new DataSourceTextField("organization", "Organization");
DataSourceTextField phoneField = new DataSourceTextField("phone", "Phone");

DataSourceTextField streetField = new DataSourceTextField("street", "Street");
streetField.setValueXPath("address/street");

DataSourceTextField cityField = new DataSourceTextField("city", "City");
cityField.setValueXPath("address/city");

DataSourceTextField stateField = new DataSourceTextField("state", "State");
stateField.setValueXPath("address/state");

DataSourceTextField zipField = new DataSourceTextField("zip", "Zip");
zipField.setValueXPath("address/zip");

dataSource.setFields(nameField, emailField, orgField, phoneField, streetField, cityField, stateField, zipField);

ListGrid grid = new ListGrid();
grid.setDataSource(dataSource);
grid.setWidth100();
grid.setHeight(100);
grid.setAutoFetchData(true);
grid.draw();

2 件のコメント:

hanishi さんのコメント...

初めまして、hanishiです。
SmartGWTの強みはサーバ側のコンポーネントにあります。Reloginメカニズムも是非、試してみてください。
http://forums.smartclient.co/showthread.php?t=9633

koma さんのコメント...

hanishi さんへ

SmartGWTサーバー側の情報をご提供いただきありがとうございます、参考します。

コメントを投稿

ホームページ