GWT and XML : 可扩展标记语言 « GWT « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » GWT » 可扩展标记语言屏幕截图 
GWT and XML

/*
 * Copyright 2006 Google Inc.
 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 
 * http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.sample.simplexml.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.HTTPRequest;
import com.google.gwt.user.client.ResponseTextHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.xml.client.Document;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.google.gwt.xml.client.NodeList;
import com.google.gwt.xml.client.XMLParser;

/**
 * A very simple XML Example where we take a customer profile and display it on
 * a page.
 */
public class SimpleXML implements EntryPoint {
  private static final String XML_LABEL_STYLE = "xmlLabel";
  private static final String USER_TABLE_LABEL_STYLE = "userTableLabel";
  private static final String USER_TABLE_STYLE = "userTable";
  private static final String NOTES_STYLE = "notes";

  public void onModuleLoad() {
    HTTPRequest.asyncGet("customerRecord.xml"new ResponseTextHandler() {
      public void onCompletion(String responseText) {
        // In the real world, this text would come as a RPC response. This
        // technique is great for testing and samples though!
        renderXML(responseText);
      }

      private FlexTable createOrderTable(FlowPanel xmlParsed, String label) {
        HTML orderTableLabel = new HTML("<h2>" + label + "</h2>");
        xmlParsed.add(orderTableLabel);
        FlexTable orderTable = new FlexTable();
        orderTable.setStyleName(USER_TABLE_STYLE);
        orderTable.setBorderWidth(3);
        orderTable.getRowFormatter().setStyleName(0, USER_TABLE_LABEL_STYLE);
        orderTable.setText(00"Order ID");
        orderTable.setText(01"Item");
        orderTable.setText(02"Ordered On");
        orderTable.setText(03"Street");
        orderTable.setText(04"City");
        orderTable.setText(05"State");
        orderTable.setText(06"Zip");
        xmlParsed.add(orderTable);
        return orderTable;
      }

      /**
       * Creates the xml representation of xmlText. xmlText is assumed to have
       * been validated for structure on the server.
       
       @param xmlText xml text
       @param xmlParsed panel to display customer record
       */
      private void customerPane(String xmlText, FlowPanel xmlParsed) {
        Document customerDom = XMLParser.parse(xmlText);
        Element customerElement = customerDom.getDocumentElement();
        // Must do this if you ever use a raw node list that you expect to be
        // all elements.
        XMLParser.removeWhitespace(customerElement);

        // Customer Name
        String nameValue = getElementTextValue(customerElement, "name");
        String title = "<h1>" + nameValue + "</h1>";
        HTML titleHTML = new HTML(title);
        xmlParsed.add(titleHTML);

        // Customer Notes
        String notesValue = getElementTextValue(customerElement, "notes");
        Label notesText = new Label();
        notesText.setStyleName(NOTES_STYLE);
        notesText.setText(notesValue);
        xmlParsed.add(notesText);

        // Pending orders UI setup
        FlexTable pendingTable = createOrderTable(xmlParsed, "Pending Orders");
        FlexTable completedTable = createOrderTable(xmlParsed, "Completed");
        completedTable.setText(07"Shipped by");

        // Fill Orders Table
        NodeList orders = customerElement.getElementsByTagName("order");
        int pendingRowPos = 0;
        int completedRowPos = 0;
        for (int i = 0; i < orders.getLength(); i++) {
          Element order = (Elementorders.item(i);
          HTMLTable table;
          int rowPos;
          if (order.getAttribute("status").equals("pending")) {
            table = pendingTable;
            rowPos = ++pendingRowPos;
          else {
            table = completedTable;
            rowPos = ++completedRowPos;
          }
          int columnPos = 0;
          fillInOrderTableRow(customerElement, order, table, rowPos, columnPos);
        }
      }

      private void fillInOrderTableRow(Element customerElement, Element order,
          HTMLTable table, int rowPos, int columnPos) {
        // Order ID
        String orderId = order.getAttribute("id");
        table.setText(rowPos, columnPos++, orderId);

        // Item
        Element item = (Elementorder.getElementsByTagName("item").item(0);
        String itemUPC = item.getAttribute("upc");
        String itemName = item.getFirstChild().getNodeValue();
        Label itemLabel = new Label(itemUPC);
        itemLabel.setTitle(itemName);
        table.setWidget(rowPos, columnPos++, itemLabel);

        // Ordered On
        String orderedOnValue = getElementTextValue(customerElement,
          "orderedOn");
        table.setText(rowPos, columnPos++, orderedOnValue);

        // Address
        Element address = (Elementorder.getElementsByTagName("address").item(
          0);
        XMLParser.removeWhitespace(address);
        NodeList lst = address.getChildNodes();
        for (int j = 0; j < lst.getLength(); j++) {
          Element next = (Elementlst.item(j);
          String addressPartText = next.getFirstChild().getNodeValue();
          table.setText(rowPos, columnPos++, addressPartText);
        }

        // Shipped By (optional attribute)
        NodeList shippedByList = order.getElementsByTagName("shippingInfo");
        if (shippedByList.getLength() == 1) {
          Element shippedBy = (ElementshippedByList.item(0);
          // Depending upon the shipper, different attributes might be
          // available, so XML carries the display info
          FlexTable shippedByTable = new FlexTable();
          shippedByTable.getRowFormatter().setStyleName(0,
            USER_TABLE_LABEL_STYLE);
          shippedByTable.setBorderWidth(1);
          NodeList shippedByParts = shippedBy.getChildNodes();
          for (int j = 0; j < shippedByParts.getLength(); j++) {
            Node next = shippedByParts.item(j);
            Element elem = (Elementnext;
            shippedByTable.setText(0, j, elem.getAttribute("title"));
            shippedByTable.setText(1, j, elem.getFirstChild().getNodeValue());
          }
          table.setWidget(rowPos, columnPos++, shippedByTable);
        }
      }

      /**
       * Utility method to return the values of elements of the form <myTag>tag
       * value</myTag>
       */
      private String getElementTextValue(Element parent, String elementTag) {
        // If the xml is not coming from a known good source, this method would
        // have to include safety checks.
        return parent.getElementsByTagName(elementTag).item(0).getFirstChild()
          .getNodeValue();
      }

      private void renderXML(String xmlText) {
     
        final TabPanel tab = new TabPanel();
        final FlowPanel xmlSource = new FlowPanel();
        final FlowPanel xmlParsed = new FlowPanel();
        tab.add(xmlParsed, "Customer Pane");
        tab.add(xmlSource, "XML Source");
        tab.selectTab(0);
        RootPanel.get().add(tab);
        xmlPane(xmlText, xmlSource);
        customerPane(xmlText, xmlParsed);
      }

      /**
       * Show the raw XML.
       
       @param xmlText
       @param xmlSource
       */
      private void xmlPane(String xmlText, final FlowPanel xmlSource) {
        xmlText = xmlText.replaceAll("<""&#60;");
        xmlText = xmlText.replaceAll(">""&#62;");
        Label xml = new HTML("<pre>" + xmlText + "</pre>"false);
        xml.setStyleName(XML_LABEL_STYLE);
        xmlSource.add(xml);
      }
    });
  }
}



           
       
GWT-SimpleXML.zip( 11 k)
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.