Pages

Friday, October 26, 2012

MGWT – Reading data from XML file on mobile phones


MGWT stands for making GWT(google web toolkit) work with mobile.
Mgwt works on all kind of mobile devices that support webkit (like iPhone, iPad, iPod, Android, Blackberry, ...) and is invented by Daniel Kurka. You can find more information and you can download this java library on http://www.m-gwt.com/

In a short presentation I would like you to show you, how can we read data from client side xml file using the java programming language.

First of all, I created a simple GWT web project and I added the Mgwt library to it. In all the GWT projects, we have a configuration file, where we must list the additional libraries and components of the project. 

Here you can see that I declared 4 package, each of them having its purpose. The first one, user.User stands for gwt libraries, mgwt.MGWT shows us that it is a porject for mobile phone that supports webkit, the xml.XML works with xml file, and 4th. one stands for date and time functions. The gwt date and time library can be downloaded from http://code.google.com/p/gwt-time/ which I also added to my project, to show you how can we use date and time functions in our mgwt project.

The content of the xml file which we will use in our project, to read data from, is looks like:
 
Reading from an xml file with the gwt/mgwt project, can be resolved on client side, that means, there is no need to any web application server, all the project runs in offline mode too. If you would like to write back data to an xml file, then you need a web application server, like Tomcat or any other to implement the server side tehchnology using GWT-RPC(remote procedure call) technology. I wouldn’t like to enter now in details, regarding server side programming, because this short presentation is just about reading offline data.

I am going to present now some lines of code form the main java file. The whole project can be downloaded from my file server, http://siposlehel.atw.hu

  1. Loading the xml file:
        try {
            RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.GET,"content.xml");                                                               
            requestBuilder.sendRequest(null, new RequestCallback() {
                public void onError(Request request, Throwable exception) { 
                    System.out.println(exception.getMessage());
                }
                public void onResponseReceived(Request request, Response response){             
                    renderXML(response.getText());                   
                }
            });
        }
        catch (Exception ex) {
            System.out.println(ex.getMessage());
        }

  1. Work with xml file – getting data from xml
    private void renderXML(String xmlText) {       
        try {
            Document messageDom = null;           
            messageDom = XMLParser.parse(xmlText);            
            NodeList entries = messageDom.getDocumentElement().getElementsByTagName("student");       
            for (int intI = 0 ; intI < entries.getLength(); intI++) {           
                Element entry = (Element) entries.item(intI);                                                
                String keyValue = entry.getElementsByTagName("id").item(0).getFirstChild().getNodeValue();               
                //Getting the record from xml with an id=1(strId)
                if (keyValue.equals(strId)) {
                    //getting the the field <name> from the xml               
                    strName = entry.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();                   
                    //getting the the field <begin> from the xml               
                    strDate = entry.getElementsByTagName("begin").item(0).getFirstChild().getNodeValue();                                                            
                }   
            }
        }
        catch(Exception e) {
            System.out.println(e.getMessage());
        }
    }

  1. using date and time utilities in our MGWT project
            DateTime dateToday = new DateTime();
            int year = dateToday.getYear();
            int month = dateToday.getMonthOfYear();
            int day = dateToday.getDayOfMonth();
            strToday = String.valueOf(year)+"."+String.valueOf(month)+"."+String.valueOf(day);                               

The running application looks like this:

No comments:

Post a Comment