permaculture playing cards*
The moose likes Java Micro Edition and the fly likes Parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Parser" Watch "Parser" New topic
Author

Parser

Zenna Lin
Greenhorn

Joined: Oct 09, 2004
Posts: 1
Can someone teach me what is Parser? And what is wrong wiht the following code:

import java.io.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;

import org.kxml.*;
import org.kxml.parser.*;

public class LoginParser
{
DVDReserMIDlet midlet;

public LoginParser(DVDReserMIDlet mid)
{
midlet = mid;
}

public void parse(final String URL)
{
Thread t = new Thread()
{
public void run()
{
// set up the network connection
HttpConnection hc = null;

try
{
System.out.println("LoginParser-"+midlet.URL);
hc = (HttpConnection)Connector.open(midlet.URL);
parse(hc.openInputStream());
}
catch (IOException ioe)
{
//mRSSListener.exception(ioe);
System.out.println("LoginParser-Error");
}
finally
{
try
{
if (hc != null)
hc.close();
}
catch (IOException ignored) {}
}
}
};
t.start();
}

public void parse(InputStream in) throws IOException
{
Reader reader = new InputStreamReader(in);
XmlParser parser = new XmlParser(reader);
ParseEvent pe = null;

String count = "";

boolean trucking = true;
while (trucking)
{
pe = parser.read();
if (pe.getType() == Xml.START_TAG)
{
String name = pe.getName();

if (name.equals("USER"))
{
pe = parser.read();
count =pe.getText();
midlet.LoginOK=count;
System.out.println("LoginParser"+midlet.LoginOK);
if(midlet.LoginOK=="1")
{
midlet.ChoiceSelect(1);
return;
}
}
else
{
while ((pe.getType() != Xml.END_TAG) || (pe.getName().equals("html") == false))
pe = parser.read();
}
}
if (pe.getType() == Xml.END_TAG && pe.getName().equals("USER"))
trucking = false;
}

}
}

This is my xml:
<?xml version="1.0" encoding="utf-8" ?>
- <USER xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<Count>1</Count>
</USER>

This is the java file:
//include the MIDlet super class
import javax.microedition.lcdui.*;

public class DVDLogin extends Form implements CommandListener
{
DVDReserMIDlet midlet;

LoginParser lp;

TextField user_id = new TextField ("User ID: ", "", 10, TextField.ANY);
TextField password = new TextField ("Password: ", "", 10, TextField.ANY);

Ticker ticker = new Ticker("Papillion's DVD - Login");

public DVDLogin (DVDReserMIDlet m)
{
super("Welcome. Enjoy your stay");
setCommandListener(this);
this.midlet = m;

try
{
setTicker(ticker);
Image image = Image.createImage("/Butterfly t1.png");
ImageItem butterfly = new ImageItem("", image, ImageItem.LAYOUT_CENTER, "Error. Image Missing!");

this.append(butterfly);
this.append(user_id);
this.append(password);

addCommand(new Command("OK", Command.OK, 1));
addCommand(new Command("Quit", Command.STOP, 1));
}
catch(Exception e)
{
e.printStackTrace();
}
}

/**Handle command events*/
public void commandAction(Command c, Displayable d)
{
if (c.getCommandType() == Command.STOP)
{
DVDReserMIDlet.quitApp();
return;
}
if (c.getCommandType() == Command.OK)
{
String URL = new String("");

URL = "http://localhost:8080/WebService.asmx?CheckUser?userid="+user_id.getString()+"&password="+password.getString();

midlet.URL = URL;

System.out.println("DVD_Login"+user_id.getString());
System.out.println("DVD_Login"+password.getString());
System.out.println("DVD_Login"+midlet.URL);

LoginParser lp_1 = new LoginParser(midlet);
lp_1.parse(midlet.URL);
}
}
}

This is the midlet file:
//include the MIDlet super class
import javax.microedition.midlet.MIDlet;
//include the GUI libraries of MIDP
import javax.microedition.lcdui.*;

public class DVDReserMIDlet extends MIDlet
{
static DVDReserMIDlet instance;

public String URL = new String ("");

//User Information
public String selectedUSER_ID = new String ("");//R
public String selectedPassword = new String ("");

//DVD Information
public String selectedDVD_ID = new String ("");//R
public String selectedTitle = new String ("");
public String selectedQty = new String ("");
public String selectedDialect = new String ("");
public String selectedDay = new String ("");

//Reservation Information
public String selectedReser_ID = new String ("");//R
public String selectedReser_Qty = new String ("");//R
public String selectedDate = new String ("");//R

//Other
public String LoginOK = new String ("");

DVDLogin dvd_login;
DVDSearch dvd_search ;
DVDResult dvd_result;
DVDReservation dvd_reservation;
DVDConfirmation dvd_confirmation;

private Display displayable;

/**Construct the midlet*/
public DVDReserMIDlet ()
{
this.instance = this;
dvd_login = new DVDLogin(this);
}

/**Main method*/
public void startApp()
{
displayable = Display.getDisplay(this);
displayable.setCurrent(dvd_login);
}

/**Handle pausing the MIDlet*/
public void pauseApp()
{
}

/**Handle destroying the MIDlet*/
public void destroyApp(boolean unconditional)
{
}

/**Quit the MIDlet*/
public static void quitApp()
{
instance.destroyApp(true);
instance.notifyDestroyed();
instance = null;
}

public void ChoiceSelect(int idx)
{
switch(idx)
{
case 0:
dvd_login = new DVDLogin (this);
displayable.setCurrent(dvd_login);
break;

case 1:
dvd_search = new DVDSearch (this);
displayable.setCurrent(dvd_search );
break;

case 2:
dvd_result = new DVDResult(this);
displayable.setCurrent(dvd_result);
break;

case 3:
dvd_reservation= new DVDReservation(this);
displayable.setCurrent(dvd_reservation);
break;

case 4:
dvd_confirmation = new DVDConfirmation (this);
displayable.setCurrent(dvd_confirmation );
break;
}

}
}
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Parser