⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dataparser.java

📁 Mobile Services
💻 JAVA
字号:
import java.io.InputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.kxml.*;
import org.kxml.parser.XmlParser;
import org.kxml.parser.*;
 
import java.util.Vector;
import java.util.Hashtable;
 
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
 
import java.lang.String;
import javax.microedition.rms.*;
public class DataParser
{
	/* HttpConnection Object */
 	HttpConnection con = null;
  	/* This InputStream Object is used to read data from the InputStream opened on a URL */
  	InputStream ins = null;
  	/* ParserEvent are event generated by the parser while parsing data */
  	ParseEvent event;
  	/* This class is an object of Abstract Xml parser  */
  	AbstractXmlParser parser;
  	/* Vector objects declared  */
  	Vector hall_id,hall_name,halldata,vnews;
  	/* HashTable objects declared */
 	Hashtable htable;
  	/* RecordStore object declared. This object is used to store data in the database */
  	RecordStore recordStore = null;	
  	
  	/* Constructor of GetData Declared  */
  	public DataParser()
  	{
		hall_id = new Vector();
  		hall_name = new Vector();
  		halldata = new Vector();
  		vnews = new Vector();	
  		htable = new Hashtable();
  		htable = new Hashtable();
  	}
  	/* This method is used to establish an http connection open an stream with the connection and
  	create a parser object and pass this stream to the parser  */
  	public void sourceurl(String url)
  	{
		try
  		{	
			/* This will create an HTPP connection object */
			con = (HttpConnection)Connector.open(url);
 			/* This will open an InputStream to the connection */
  			ins = con.openInputStream();
  			/* This is initializing  a parser object  */
  			parser = new XmlParser(new InputStreamReader(ins));
  		}
  		catch(IOException ex)
  		{
  			System.out.println("IOException occured");
  		}	
  	}
  	
  	void read_category()
  	{
  		boolean foundnews = false;
  		do
  		{
  			try
  			{
  
  				event = parser.read ();
  				if(event.getType()==Xml.START_TAG)
  				{
  					
  					/* get the tag value*/
  					StartTag stag = (StartTag)event;
  
  					/* tag value is equal to category then enter in this if condition */
  					if(stag.getName().equals("category"))
  					{
  						/* foundnews veriable true */
  						foundnews = true;
  					}
  				}	
  				if(event.getType()== Xml.TEXT)
  				{
  					
  					TextEvent tevent = (TextEvent)event;
  					if(foundnews)
  					{
  						vnews.addElement(tevent.getText());
  						foundnews = false; 
  					}
  				}
  			}
  			catch(IOException ex)
  			{	
  				System.out.println("Exception occured");
  			}
  		}
  		while (!(event instanceof EndDocument));	
  	}	
  	
  	void read_record()
  	{
  		int k, id;
  		String state = "", data = "";
  		boolean founditem = false, foundstate = false;
  		try 
		{
  			/*  Opens RecordStore for insertion of records */
  			recordStore = RecordStore.openRecordStore("addresses", true);
		}
		catch(RecordStoreException rse) 
	  	{
			rse.printStackTrace();
	    }
  		do
  		{
  			try
  			{
  				/* Events generated by the parser while parsing XML file */			
  				event = parser.read ();
  				
  				/* Type of event generated while parsing XML File */
  				if(event.getType()==Xml.START_TAG)
  				{
  					StartTag stag = (StartTag)event;
  					if(stag.getName().equals("weather"))
  					{
  						founditem = true;
  					}
  					if(stag.getName().equals("state"))
  					{
  						foundstate = true;
  					}
  				}	
  				if(event.getType()== Xml.TEXT)
  				{
  					TextEvent tevent = (TextEvent)event;
  					if(foundstate)
  					{
  						state =  tevent.getText();
  						data = data+"?"+state;
  						foundstate = false; 
  					}
  					else
  					{
  						data = data+"?"+ tevent.getText();
  					}
  				}
  			
  				if(event.getType()==Xml.END_TAG)
  				{
  					EndTag etag = (EndTag)event;
  					if(etag.getName().equals("weather"))
  					{
  						data = data+"?";
  						founditem = false;
  						/* Calling the method for insertion of record into the database */
  						id = record_add(data);
  						/* insertion of record into the hashtable */
  						htable.put((Object)state, Integer.toString(id));
  						data = "";
  					}
  				}
  			}
  			catch(IOException ex)
  			{	
  				System.out.println("Exception occured");
  			}
  		}
  		while (!(event instanceof EndDocument));	
  		try 
  		{
  			/*  Closes  RecordStore after insertion of records */
          	recordStore.closeRecordStore();	
  		}
  		catch(RecordStoreException rse) 
  		{
  			rse.printStackTrace();
  		}
  			
  	}
  	/* Function for record addtion...*/
  	int record_add(String data)	 
  	{
  		int i = 0;
  		try 
  		{
  			byte b[] = data.getBytes();
  			i = recordStore.addRecord(b,0, b.length);
  		}
  		catch (RecordStoreException rse)
  		{
  	       rse.printStackTrace();
  		}
  		return i ;
  	}  
  
  	/* Method for deletion of records from the Recordstore  */
  	void deleterecords()
  	{
  		try 
  		{
  			recordStore.deleteRecordStore("addresses");
  		}
  		catch (RecordStoreException rse)
  		{
  	       rse.printStackTrace();
  		}
  		
  	}
  	void read_news()
  	{
  		int k, id;
  		String news = "", data = "", head_id = "", head = "";
  		boolean foundnews = false, foundhead = false, foundhead_id = false;
  		do
  		{
  			try
  			{
  				event = parser.read ();
  				if(event.getType()==Xml.START_TAG)
  				{
  					StartTag stag = (StartTag)event;
  					if(stag.getName().equals("news"))
  					{
  						foundnews = true;
  					}
 					if(stag.getName().equals("heading"))
  					{
  						foundhead = true;
  						
  					}
  					if(stag.getName().equals("heading_id"))
  					{
  						foundhead_id = true;
  					}
  				}	
  				if(event.getType()== Xml.TEXT)
  				{
 					TextEvent tevent = (TextEvent)event;
  					if(foundhead_id)
  					{
  						head_id  =  tevent.getText();
  						foundhead_id = false; 
  					}
  					if(foundhead)
  					{
  						head  =  tevent.getText();
  						foundhead = false; 
  					}
  				}
  				if(event.getType()==Xml.END_TAG)
  				{
  					EndTag etag = (EndTag)event;
  					if(etag.getName().equals("news"))
  					{
  					
  						foundnews = false;
  						htable.put((String)head, (Object)head_id);
  						head = "";
  					}
  				}
  			}
  			catch(IOException ex)
  			{	
  				System.out.println("Exception occured");
  			}
  		}
  		while (!(event instanceof EndDocument));	
  	}
  	String returnDetails()
  	{
  		int k, id;
  		String news = "", data = "", description = "";
  		boolean found_description = false;
  		do
  		{
  			try
  			{
  				event = parser.read ();
  				if(event.getType()==Xml.START_TAG)
  				{
  					StartTag stag = (StartTag)event;
  					if(stag.getName().equals("description"))
  					{
  						found_description = true;
  					}
  				}	
  				if(event.getType()== Xml.TEXT)
  				{
  					TextEvent tevent = (TextEvent)event;
  					if(found_description)
  					{
  						description  =  tevent.getText();
  						found_description = false; 
  					}
  				}
  			}
  			catch(IOException ex)
  			{	
  				System.out.println("Exception occured");
  			}
  		}
  		while (!(event instanceof EndDocument));	
  		return description ; 	
  	}
  	/* This method is called by the MovieList class this method parser data related to movies */
  	void parseDataMovies(int i)
  	{
  		boolean foundhallid = false;
  		boolean hall = false;
  		boolean moviename = false;
  		boolean moviestatus = false;
  		boolean moviedate = false;
  		boolean showtime = false;
  		boolean balcony = false;
  		boolean rear = false;
  		boolean upper = false;
  		boolean middle = false;
  		/* This if condition is true when this method is called first time. ie when data related 
  		to hallname and hallid is to read */
  		if(i == 1)
  		{
  			do
  			{
  				try
  				{
  					
  				/* This will read the data form the stream and generate parsing events  */
  					event = parser.read ();
  					if(event.getType()==Xml.START_TAG)
  					{
  						StartTag stag = (StartTag)event;
  						if(stag.getName().equals("hall_id"))
  						{
  							foundhallid = true;
  						}
  						if(stag.getName().equals("hall_name"))
  						{	
  							hall = true;
  						}
  					
  					}
  					/* This will be true if the there is some Text found */
  					if(event.getType()== Xml.TEXT)
  					{
  						TextEvent tevent = (TextEvent)event;
  						if(foundhallid)
  						{
  							hall_id.addElement(tevent.getText());
  							foundhallid = false;
  						}
  						if(hall)
  						{
  							hall_name.addElement(tevent.getText());
  							hall = false;
  						}
  					}
  				}
  				catch(IOException ex)
  				{	
  					System.out.println("Exception occured");
  				}	
  			}
  			while (!(event instanceof EndDocument));	
  		}				
  		/* This if condition is true when this method is called second time. ie when data related 
  		to a particular hall is read */
  		if(i == 2)
  		{
  			do
  			{
  				try
  				{
  					
  				/* This will read the data form the stream and generate parsing events  */
  					event = parser.read ();
  					if(event.getType()==Xml.START_TAG)
  					{
  						StartTag stag = (StartTag)event;
 						if(stag.getName().equals("movie_name"))
  						{
  							moviename = true;
  							halldata.addElement("ms");
  						}
  						if(stag.getName().equals("status"))
  						{	
  							moviestatus = true;
  							halldata.addElement("st");
  
  						}
  						if(stag.getName().equals("movie_date"))
  						{	
  							moviedate = true;
  						}
  						if(stag.getName().equals("showtime"))
  						{	
  							showtime = true;
 						}	
  						if(stag.getName().equals("balcony"))
  						{	
  							balcony = true;
  						}
  						if(stag.getName().equals("rear_stall"))
  						{	
  							rear = true;
  						}
  						if(stag.getName().equals("upper_stall"))
  						{	
  							upper = true;
 						}
 						if(stag.getName().equals("middle_stall"))
  						{	
  							middle = true;
  						}
  					}
  					
  					if(event.getType()== Xml.TEXT)
  					{
  						TextEvent tevent = (TextEvent)event;
  						if(moviename)
  						{
  							halldata.addElement(tevent.getText());
  							moviename = false;
  						}
  						if(moviestatus)
  						{
  						
  							halldata.addElement(tevent.getText());
  							moviestatus = false;
  						}
  						if(moviedate)
  						{
  							halldata.addElement(tevent.getText());
  							moviedate = false;
  						}
  						if(showtime)
  						{
  							halldata.addElement(tevent.getText());
  							showtime = false;
  						}
  						if(balcony)
  						{
  							halldata.addElement(tevent.getText());
  							balcony = false;
  						}
  						if(rear)
  						{
  							halldata.addElement(tevent.getText());
  							rear = false;
  						}
  						if(upper)
  						{
  							halldata.addElement(tevent.getText());
  							upper = false;
  						}
  						if(middle)
  						{
  							halldata.addElement(tevent.getText());
  							middle = false;
  						}
  					}	
  				}
  				catch(IOException ex)
  				{	
  					System.out.println("Exception occured");
  				}	
  			}
  			while (!(event instanceof EndDocument));	
  		}
  	}	
  	/* This method will release the memory and free the resources */
  	void releaseMeromery()
  	{
  		con = null;
  		ins = null;
  		parser = null;
  		hall_name = null;
  		hall_id = null;
  		
  	}
  	
  	/* The next three method will return vector objects containing itemcode,
  	   item quantity and item price respectively  */
  	Vector hall_id()
  	{
  		return hall_id;
  	}
  	
  	Vector hall_name()
  	{
  		return hall_name;
  	}
  	
  	Vector halldata()
  	{
  		return halldata;
  	}
  	
  	Vector returnNews()
 	{
  		return vnews;
 	}
  	/* This method will return Hashtable object containing values*/	
  	Hashtable returnHash_id()
  	{
  		return htable;
  	}	
  	/* This method will return Hashtable object containing values */	
  	Hashtable returnState()
  	{
  		return htable;
  	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -