Pages

Tuesday, November 6, 2012

Mobile phones with Codename One – Getting/Storing data from/into xml file



Today I found a short description on wikipedia about how many technologies are in mobile application development area.

You can choose your programming language that you prefer to use, and you will find which mobile platform you can address with it.

The Codename One  technology uses the java language and enables Java Developers to build true native applications for all mobile/tablet platforms. Sounds quite interesting and it is. However, for the J2ME enabled phones, personally I prefer to use the j2me language and environment with the Netbeans IDE.

But let see through a short example, what is Codename One. It declares that you have just 5 easy steps for app development:
-         downloading the codename one plugin for free (eclipse or netbeans)
-         write your code in java
-         you have a designer for the GUI
-         and you have an embedded simulator to test your app
-         Generate a native mobile application for the desired device, sending to the build server, your code to be build. (you can do this from your IDE)

You can have more information at http://www.codenameone.com/


Now, let’ see a short example code, how to read and write xml file with codename one.
After you create a codename one project in your ide, just put a button and a label onto your form using the designer.
I used the following xml file for this example:

You must import packages like:
import com.codename1.io.*;
import com.codename1.ui.*;
import com.codename1.ui.events.*;
import com.codename1.xml.*;
//import java.io.InputStreamReader;
At the button’s onclick event I associated the following code for reading XML files, and also you will see the "xml write" in commented lines:
    protected void onMain_BtnDataAction(Component c, ActionEvent event) {                    
        String fromXml = "empty";
        try {
            //open the xml file for reading
            BufferedInputStream file = new BufferedInputStream(
                    FileSystemStorage.getInstance().openInputStream("example.xml"));
            InputStreamReader reader = new InputStreamReader(file);         
           
            //processing the xml
            XMLParser parser = new XMLParser();           
            Element elem;    
            elem = parser.parse(reader);   
            fromXml = elem.getChildAt(0).getChildAt(0).getChildAt(0).getText();            
/*elem.getChildAt(0).getChildAt(0).getChildAt(0).setText("writebackintoxmlfile"); FileSystemStorage.getInstance().openOutputStream("example.xml").write(elem.toString().getBytes());*/
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
        }       
        //label component - setting it's text from the XML
        findLblData().setText("XML: "+fromXml);       
        Dialog.show("Hello world","Success reading XML","OK",null);               
    }

The result, running your program:

1 comment:

  1. Where would you placed your example.xml in? src folder? Because the application can't located the file.

    ReplyDelete