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
.
SimpleXML.java
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
RequestBuilder(RequestBuilder.GET,"customerRecord.xml");
try {
requestBuilder.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
requestFailed(exception);
}
public void onResponseReceived(Request request, Response response) {
renderXML(response.getText());
}
});
} catch (RequestException ex) {
requestFailed(ex);
}
}
private FlexTable createOrderTable(FlowPanel xmlParsed, String label){
HTML orderTableLabel = new HTML("<h2>" + label);
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(0, 0, "Employee ID");
orderTable.setText(0, 1, "Firstname");
orderTable.setText(0, 2, "Lastname");
orderTable.setText(0, 3, "City");
orderTable.setText(0, 4, "State");
orderTable.setText(0, 5, "Zip");
xmlParsed.add(orderTable);
return orderTable;
}
private void customerPane(String xmlText, FlowPanel xmlParsed) {
Document customerDom = XMLParser.parse(xmlText);
Element customerElement = customerDom.getDocumentElement();
XMLParser.removeWhitespace(customerElement);
String nameValue = getElementTextValue(customerElement, "name");
String title = "<h1>" + nameValue + "</h1>";
HTML titleHTML = new HTML(title);
xmlParsed.add(titleHTML);
String notesValue = getElementTextValue(customerElement, "notes");
Label notesText = new Label();
notesText.setStyleName(NOTES_STYLE);
notesText.setText(notesValue);
xmlParsed.add(notesText);
FlexTable pendingTable = createOrderTable(xmlParsed, "Employee Details");
NodeList orders = customerElement.getElementsByTagName("order");
int pendingRowPos = 0;
int completedRowPos = 0;
System.out.println(orders.getLength());
for (int i = 0; i < orders.getLength(); i++) {
Element order = (Element) orders.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) order.getElementsByTagName("item").item(0);
String itemUPC = item.getAttribute("Firstname");
String itemName = item.getFirstChild().getNodeValue();
Label itemLabel = new Label(itemUPC);
itemLabel.setTitle(itemName);
table.setWidget(rowPos, columnPos++, itemLabel);
String orderedOnValue = getElementTextValue(customerElement, "Lastname");
table.setText(rowPos, columnPos++, orderedOnValue);
Element address = (Element) order.getElementsByTagName("address").item(0);
XMLParser.removeWhitespace(address);
NodeList lst = address.getChildNodes();
for (int j = 0; j < lst.getLength(); j++) {
Element next = (Element) lst.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).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, "Employee");
tab.add(xmlSource, "XML");
tab.selectTab(0);
RootPanel.get().add(tab);
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("<", "<");
xmlText = xmlText.replaceAll(">", ">");
Label xml = new HTML("<pre>" + xmlText + "</pre>", false);
xml.setStyleName(XML_LABEL_STYLE);
xmlSource.add(xml);
}
}
|
Step2:-Create a xml file named customerRecord.xml.
customerRecord.xml
<?xml version="1.0" encoding="US-ASCII"?>
<CustomerRecord>
<name>Girish Tewari</name>
<notes>Roseindia.net Rohini</notes>
<orderHistory>
<order id="05mca02" status="pending">
<item Firstname="Girish">Bronze Sword</item>
<address>
<city>Haldwani</city>
<state>Uttaranchal</state>
<zipCode>263126</zipCode>
</address>
<Lastname>Tewari</Lastname>
</order>
</orderHistory>
<name>Mahendra Singh Dhoni</name>
<notes>Roseindia.net Rohini</notes>
<orderHistory>
<order id="05mca03" status="pending">
<item Firstname="Mahendra">Bronze Sword</item>
<address>
<city>Lucknow</city>
<state>UttarPradesh</state>
<zipCode>263139</zipCode>
</address>
<Lastname>Tewari</Lastname>
</order>
</orderHistory>
</CustomerRecord>
|
|
Step3:-Create html file named SimpleXML.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<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">
</head>
<body bgcolor="white">
<script type="text/javascript" language="javascript"
src="com.google.gwt.sample.simplexml.SimpleXML.nocache.js">
</script>
<h1>Retrieving XML Data Using GWT</h1>
<p>
Example of loading an XML file from the server.
</p>
</body>
</html>
|
|
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
