普通的JAVA XML解析包比较大,占用资源比较多,不适合嵌入式设备使用
KXML是专门用在嵌入式设备上,只有十几K
下面是我在实际项目中用到的一代码,供大家参考
public String XmlDecoding(DataInputStream din) throws IOException{
String temp = null;
Reader reader = new InputStreamReader(din);
XmlParser parser = new XmlParser(reader);
ParseEvent pe = null;
parser.read(Xml.START_TAG, null, "upos");
parser.skip();
ParseEvent event = null;
do {
try {
event = parser.read ();
if(event.getType()==Xml.START_TAG) {
String name = event.getName();
temp += "<"+name + ">/n";
}
if(event.getType()== Xml.TEXT) {
String name = event.getText();
name = name.trim();
temp += name + "/n";
}
if(event.getType()== Xml.END_TAG) {
String name = event.getName();
temp += "<"+name + ">/n";
}
}
catch(IOException ex) {
System.out.println("Exception occured");
}
}
while (event.getType() != Xml.END_DOCUMENT);
return temp;
}
一段如何使用kxml的代码- -
到处找kxml的代码,没找到,刚巧看到《Crack J2ME》里面有一段,非常完整,贴下来供同道参考吧。关于详细的解释,可以参加这一篇文章
XMLCaseStudy
/**
* 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.
}
}
这一段是result_screen.java
/**
* Basic packages used by various classes..
*
*/
import com.sun.kjava.*;
/**
* Decalaring a class result_screen which is responsible for
* showing the results in the ScrollTextBox
*/
public class result_screen extends Spotlet
{
/**
* Declaring the GUI components
*
*/
Button exit = new Button("Exit", 80,145);
ScrollTextBox result_box;
result_screen(String result)
{
/**
* Getting the Graphics and redrawing the screen.
*
*/
Graphics graphics = Graphics.getGraphics();
graphics.resetDrawRegion();
graphics.clearScreen();
/**
* Painting the title on the screen.,
*
*/
graphics.drawString("Result of parsing",25,2);
/**
* Registering the Spotlet and painting the GUI components.
*
*/
result_box = new ScrollTextBox(result,10,20,120,120);
exit.paint();
result_box.paint();
register(NO_EVENT_OPTIONS);
}
public void penDown(int x, int y)
{
/**
* Exit the program
*
*/
if (exit.pressed(x,y))
{
System.exit(0);
}
else if (result_box.contains(x,y))
result_box.handlePenMove(x,y);
}
public void penMove(int x, int y)
{
if (result_box.contains(x,y))
result_box.handlePenMove(x,y);
}
}