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

📄 xmlcasestudy.java

📁 J2ME 开发中用到的XML语法分析程序,可供学习参考使用.
💻 JAVA
字号:
/**
 *  Basic packages used by various classes.. 
 *
 */ 
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Enumeration;
import com.sun.kjava.*;
import java.lang.*;
import javax.microedition.io.*;
import java.io.*;
/**
 *  Packages used by the XML parser... 
 *
 */ 
import org.kxml.*;
import org.kxml.io.*;
import org.kxml.parser.*;

/**
 *  Declaring a class XmlCaseStudy. This class is responsible for 
 *  reading the file  and pasing it.Then it displays a screen which 
 *	contains the parse results.
 */ 
public class XmlCaseStudy extends Spotlet 
{

/**
 *   declaring the variables used by the parser.. 
 *
 */ 
AbstractXmlParser xmlparser;
ParseEvent event;
DataInputStream din;
String result_string = "";
/**
 *  Declaring the GUI components 
 *
 */ 

private Button bt = new Button("Ok",70,145);	    // Gui Components (Button)
private Button exit = new Button("Exit",35,145);   // Gui Components (Button)

/**
 *  main Class which calls the Class XmlCaseStudy. 
 *
 */ 
	  public static void main(String args[])throws Exception
      {
	    XmlCaseStudy casestudy = new XmlCaseStudy();
	  }	


		public XmlCaseStudy()
		  {
/**
 *  Getting the Graphics and redrawing the screen. 
 *
 */  
			Graphics graphics = Graphics.getGraphics();
			graphics.resetDrawRegion();
			graphics.clearScreen();	
/**
 *  Painting the title on the screen.,  
 *
 */ 
			graphics.drawString("XML case Study..",25,10);
			graphics.drawString("Press OK to fetch XML file and Parse it",5,40);
/**
 *  Registering the Spotlet and painting the GUI components.  
 *
 */ 
			register(NO_EVENT_OPTIONS);
			bt.paint();
			exit.paint();
				  
          }
		 

		    public void penDown(int x, int y)    
			{
/**
 *  When Ok is pressed unregister the spotlet, call the startReading 
 *  class to read the XML file then call the XML parser which will 
 *	parse the file and store the results in a string which is passed 
 *	as a parameter to another class which displays the result.
 */ 
				if(bt.pressed(x,y))
				 {
					unregister();
					startReading();
					parseData();
			   	    new result_screen(result_string);
				 }
				 else
/**
 *  Exit the program 
 *
 */ 
				  {
				    System.exit(0);
				  }
			}

		public void startReading()
		{
		  try
		  {
/**
 *  Read the file and pass to the XML parser.. 
 *
 */ 
			 din = Connector.openDataInputStream("testhttp://pankaj/books.xml");

			 xmlparser = new XmlParser(new InputStreamReader(din));
		  }
		  catch(IOException e)
		  {
		  System.out.println("Exception Occurred while reading");
		  }
	    }

		void parseData()
		 {
		  do
		  {
		   try
		   {
		        event = xmlparser.read ();
/**
 *  Start Tag is encountered.. and Appended to a string. 
 *
 */ 
				if(event.getType()==Xml.START_TAG)
				{
					StartTag stag = (StartTag)event;
			 	    String name = stag.getName();
					result_string = result_string + "Start "+name + "\n";
				}
/**
 *  text between tags is encountered and appended to the String. 
 *
 */ 
				if(event.getType()== Xml.TEXT)
				{
					TextEvent tevent = (TextEvent)event;
					String name = tevent.getText();
					name = name.trim();
					result_string = result_string +"Value  "+name + "\n";
				}
/**
 *  End Tag is encountered.. and Appended to a string. 
 *
 */ 

				if(event.getType()== Xml.END_TAG)
				{
					EndTag end_tag = (EndTag)event;
					String name = end_tag.getName();
					result_string = result_string + "End  "+name + "\n";
				}

		   }
		   catch(IOException ex)
		   {		
				System.out.println("Exception occured");
		   }
		}
		while (!(event instanceof EndDocument));
		System.out.println("**** END OF DOCUMENT ****"); // End od document is reached.
     }
}	  

⌨️ 快捷键说明

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