Home Tutorials Swt Retrieving XML Data Using GWT

Related Tutorials


Share on Google+Share on Google+

Retrieving XML Data Using GWT

Retrieving XML Data Using GWT

Retrieving XML Data Using GWT


This Example Describes the way of retrieving XML file Data from the server using GWT. The basic building block for running this example which we are describing below is explained here in stepwise:-

Step1:-Create a java File named SimpleXML.java. Put this File in the package com.google.gwt.sample.simplexml.client.


package com.google.gwt.sample.simplexml.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
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;

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() {
  RequestBuilder requestBuilder = new 
  try {
    requestBuilder.sendRequest(null, new RequestCallback() {
 public void onError(Request request, Throwable exception) {
  public void onResponseReceived(Request request, Response response){
  catch (RequestException ex) {
private FlexTable createOrderTable(FlowPanel xmlParsed, String label){
  HTML orderTableLabel = new HTML("<h2>" + label);
  FlexTable orderTable = new FlexTable();
  orderTable.getRowFormatter().setStyleName(0, USER_TABLE_LABEL_STYLE);
  orderTable.setText(00"Employee ID");
  return orderTable;
  private void customerPane(String xmlText, FlowPanel xmlParsed) {
  Document customerDom = XMLParser.parse(xmlText);
  Element customerElement = customerDom.getDocumentElement();

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

  String notesValue = getElementTextValue(customerElement, "notes");
  Label notesText = new Label();

  FlexTable pendingTable = 
(xmlParsed, "Employee Details");
   NodeList orders = 
  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 = pendingTable;
  rowPos = ++pendingRowPos;
  int columnPos = 0;
  fillInOrderTableRow(customerElement, order, table, rowPos, columnPos);

  private void fillInOrderTableRow
   (Element customerElement, Element order,

  HTMLTable table, int rowPos, int columnPos)

  String orderId = order.getAttribute("id");
  table.setText(rowPos, columnPos++, orderId);

  Element item = (Element
  String itemUPC = item.getAttribute("Firstname");
  String itemName = item.getFirstChild().getNodeValue();
  Label itemLabel = new Label(itemUPC);
  table.setWidget(rowPos, columnPos++, itemLabel);

  String orderedOnValue = getElementTextValue
  table.setText(rowPos, columnPos++, orderedOnValue);

  Element address = (Element
  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);

private String getElementTextValue(Element parent, String elementTag){
  return parent.getElementsByTagName(elementTag).item(0)
  private void renderXML(String xmlText) {
  final TabPanel tab = new TabPanel();
  final FlowPanel xmlSource = new FlowPanel();
  final FlowPanel xmlParsed = new FlowPanel();
  tab.add(xmlParsed, "Employee");
  tab.add(xmlSource, "XML");
  xmlPane(xmlText, xmlSource);
  customerPane(xmlText, xmlParsed);

  private void requestFailed(Throwable exception) {
  Window.alert("Failed to send the message: "
  + exception.getMessage());
  private void xmlPane(String xmlText, final FlowPanel xmlSource) {
  xmlText = xmlText.replaceAll("<""&#60;");
  xmlText = xmlText.replaceAll(">""&#62;");
  Label xml = new HTML("<pre>" + xmlText + "</pre>"false);

Step2:-Create a xml file named customerRecord.xml.


<?xml version="1.0" encoding="US-ASCII"?>
  <name>Girish Tewari</name>
  <notes>Roseindia.net Rohini</notes>
  <order id="05mca02" status="pending">
  <item Firstname="Girish">Bronze Sword</item>
  <name>Mahendra Singh Dhoni</name>
  <notes>Roseindia.net Rohini</notes>
  <order id="05mca03" status="pending">
  <item Firstname="Mahendra">Bronze Sword</item>

Step3:-Create html file named SimpleXML.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Simple XML Example</title>
  <link rel="stylesheet" type="text/css" href="SimpleXML.css">
  <body bgcolor="white"
  <script type="text/javascript" language="javascript"
  <h1>Retrieving XML Data Using GWT</h1>
  Example of loading an XML file from the server.

Another way for running this example is to download this example, Extract it and put the extracted folder in the GWT directory. Place this extracted folder in the samples directory of the GWT toolkit. Just double click on the cmd SimpleXML-shell.cmd to view the output of the program.

output of the program


Download source code



If you enjoyed this post then why not add us on Google+? Add us to your Circles

Liked it!  Share this Tutorial

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: Retrieving XML Data Using GWT  

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image