J2ME Learning Notes (Seven) ----- J2ME and XML Integration

zhaozj2021-02-16  150

J2ME and XML integration

1. XML (EXTENSIBLE MARKUP LANGUAGE) Scalable Markup Language ----- XML ​​is independent of hardware and software, XML files, and HTML files, actually a text file. The most common tool to create an XML file is the "Notepad". In addition to "Notepad", there are other more convenient tools, such as XML Notepad, XML Pro, CLIP! XML SPY, etc., a major feature of these tools is to check if the XML file you established meets the XML specification.

2. In order to access the XML file, the J2ME toolbox must contain an XML analysis program, and the XML analysis program for mobile devices is that there is no need to consume a lot of memory (less memory).

3. The XML analysis program for mobile devices has the following: (XML analysis program needs to be downloaded from the Internet)

KXML Minxml Nanoxml Tinyxml

4. Analyze the XML files There are two ways: event-based and tree-based.

Event-based XML analysis programs --- Analyze each element in the XML file separately, and send the data to the MIDlet application through the callback method.

Based on the tree-based XML analysis program --- Conversely, read the entire XML file to an internal tree structure, store this file in the memory of the mobile device, the disadvantage is that the memory overhead on the device is, the advantage is Fast and convenient navigation and manipulating the analyzed data.

5. An instance ----- Connect the MIDlet to XML file with the tree-based XML analysis program

1) First of all, a TinyXML analysis program can be downloaded from the Internet. The program is a .jar package, actually a Java class library (very small 15KB)

2) After downloading, put the TinyXML analysis program into x: / wtk104 / apps / projectname / lib.

3) The XML file is as follows:

developer

students

Faculty

30

Integration

This is j2me !!!!!!

Save as mail.xml, you can also use your own XML file ----- put the file in the PUBLIC_HTML folder of the J2EE server.

4) The 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 are now in the XML analysis program Tinytreedemo

Import gd.xml. *;

Import gd.xml.tiny. *;

Public Class Tinytreedemo Extends MIDlet ImmancellListener

{

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 the web site of the mail.xml file

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

{

/ / Return to XML root elements

Root = tinyparser.parsexml (URL);

DISPLAYNODE (root);

}

Catch (Parsexception E)

{

System.err.println ("Startapp: E);

}

}

}

Private Void DisplayNode (ParsEDXML PX)

{

// Return to Node Object Types

String nodename = px.gettypename ();

/ / Return to Node Object Type Name, such as tag , tag is type (label), mail is a node name

IF (px.getname ()! = null)

Nodename = "<" PX.GETNAME () ">";

// Return to the content stored between the label

String nodecontent = px.getcontent ();

IF (NodeContent == NULL)

Nodecontent = "";

// Print out in the console

System.out.println (NodeName ":");

System.out.println (nodecontent);

ENUMERATION E;

// Return to the property, if you are stored in the enumeration

e = px.attributes ();

IF (E.haASMoreElements ())

{

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

While (E.hasMoreElements ())

{

// Return the attribute name

String attrname = (string) E.NEXTELEMENT ();

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

System.out.println (AttrName ":" PX.GetaTRibute (attrname);}

}

/ / Return to the elements in the node, if you are stored in the enumeration

e = px.elements ();

IF (E.haASMoreElements ())

{

// Show next node

While (E.hasMoreElements ())

DisplayNode ((PARSEDXML) E.NEXTELEMENT ());

}

}

}

Save as Tinytreedemo.java

5) Compile --- Pre-verification ---- Test (J2EE server needs to be opened)

The result is as follows, the console shows the following information ----

root:

Tag :

Tag :

TEXT:

Developer

Tag :

TEXT:

Students

Tag :

TEXT:

Faculty

Tag :

TEXT:

30

Tag :

TEXT:

Integration

Tag :

Attribute: Language: ENGLISH

TEXT:

This is j2me !!!!!!

转载请注明原文地址:https://www.9cbs.com/read-9063.html

New Post(0)