How the J2ME Learning Series integrates J2ME with XML

Source: Internet
Author: User
Tags mail advantage

1, XML (extensible Markup Language) Extensible Markup Language?? XML is independent of hardware and software, and XML files, like HTML files, are actually a text file. The most common tool for creating XML files is "Notepad", just like HTML. In addition to Notepad, there are certainly more convenient tools, such as XML Notepad, XML Pro, CLIP, XML Spy, and so on, one of the features of these tools is the ability to check that the XML file you have created conforms to the XML specification.

2. In order to access the XML file, the J2ME Toolbox must contain an XML analyzer, and the advantage of the XML Analyzer for mobile devices is that it does not consume a large amount of memory (less memory).

3. XML Analyzer applications for mobile devices include the following: (XML Analyzer needs to download from the Internet)

Kxml Minxml Nanoxml TinyXML

4. There are two ways to parse XML files: event-based and tree based.

Event-based XML Analyzer?? Analyze each element in the XML file separately and send the analyzed data to the MIDlet application through the callback method.

Tree-based XML analyzer?? Instead of reading the entire XML file into an internal tree structure, storing the file in the memory of the mobile device, the disadvantage is that it brings a large memory overhead on the device and the advantage of navigating and manipulating the parsed data quickly and easily.

5, an example?? Connects MIDlet to XML files with a tree based XML analyzer.

1 first need a TinyXML analysis program, which can be downloaded from the Internet, the program is a. jar package, is actually a Java class library (very small 15KB).

2 after downloading the TinyXML analysis program into the X:\WTK104\apps\ProjectName\lib.

3 The contents of the XML file are as follows:

<?xml version="1.0" ?>
<mail>
<From>Developer</From>
<To>Students</To>
<Cc>Faculty</Cc>
<Date>30 </Date>
<Subject>Integration</Subject>
<Body Language="English">
This is J2ME!!!!!!
</Body>
</mail>

Save As Mail.xml, you can also use your own XML file?? Put the file in the Public_html folder of the Java server.

4) MIDlet application code is as follows:

import java.io.*;


import java.util.*;


import java.lang.String;


import javax.microedition.lcdui.*;


import javax.microedition.io.*;


import javax.microedition.midlet.*;


//The following two packages appear in the XML Analyzer Tinytreedemo


import gd.xml.*;


import gd.xml.tiny.*;


public class Tinytreedemo extends


MIDlet implements Commandlistener


{


private String URL;


private Parsedxml root;


private Display Mydisplay=null;


private Form Mainscreen;


private TextField Requestfield;


Command sendcommand=new command


("SEND", command.ok,1);


public Tinytreedemo ()


{


//Place mail.xml file Web site


url= "Http://127.0.0.1:8000/mail.xml";


Mydisplay=display.getdisplay (this);


mainscreen=new Form ("Type a URL:");


requestfield=new TextField


(Null,url,100,textfield.url);


mainscreen.append (Requestfield);


Mainscreen.addcommand (SendCommand);


Mainscreen.setcommandlistener (this);


}


public void startApp ()


throws Midletstatechangeexception


{


mydisplay.setcurrent (Mainscreen);


    }


public void Pauseapp ()


{


    }


public void Destroyapp


(Boolean unconditional)


    {


    }


public void Commandaction


(Command c,displayable s)


    {


if (C==sendcommand)


        {


String urlstring=


requestfield.getstring ();


Try


{


//Returns the XML root element


root=tinyparser.parsexml (URL);


Displaynode (root);


}


catch (parseexception e)


{


System.err.println ("startApp:" + e);


}


        }


    }


private void Displaynode


(parsedxml px)


    {


//Return node object type


String nodename=px.gettypename ();


//Return node object type + name, such as Tag&lt;mail&gt;,


tag is type (label), Mail is a section roll name


if (Px.getname ()!=null)


nodename+= "&lt;" + px.getname () + "&gt;";


//Returns the content stored between tags


String nodecontent=px.getcontent ();


if (nodecontent==null)


nodecontent= "";


//Print out in the console


System.out.println (nodename + ":");


System.out.println (nodecontent);


enumeration E;


//Returns properties, if any, stored in enumeration


e=px.attributes ();


if (e.hasmoreelements ())


{


System.out.print ("attribute:");


while (e.hasmoreelements ())


{


//Return property name


string Attrname= (String) e.nextelement ();


//px.getattribute (attrname) Returns the value of the property


System.out.println (attrname +


":" + Px.getattribute (attrname));


}


}


//Return elements in a node,


is stored in enumeration if it is available


E=px.elements ();


if (e.hasmoreelements ())


{


//Show Next node


while (e.hasmoreelements ())


Displaynode (


(Parsedxml) e.nextelement ());


}


    }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.