要使用kxml,你必须首先得到一个XmlParser实例,它用Reader作为构造器的参数:
try {
Reader r = .....;
XmlParser parser = new XmlParser( r );
}
catch( java.io.IOException e ){
// handle exception....
}
如果你的xml文档存储在String内的话你可以使用ByteArrayInputStream和InputStreamReader:
String xml = "<a>some xml</a>";
ByteArrayInputStream bin =
new ByteArrayInputStream( xml.getBytes() );
XmlParser parser = new XmlParser( new InputStreamReader( bin ) );
当从网上接收数据的时候可以这样:
HttpConnection conn = .....;
InputStreamReader doc =
new InputStreamReader( conn.openInputStream() );
XmlParser parser = new XmlParser( doc );
得到parser实例后我们就可以调用read方法进行解析了,read方法会返回一个ParseEvent,通过判断他的类型我们就可以解析xml了。
try {
boolean keepParsing = true;
while( keepParsing ){
ParseEvent event = parser.read();
switch( event.getType() ){
case Xml.START_TAG:
..... // handle start of an XML tag
break;
case Xml.END_TAG:
..... // handle end of an XML tag
break;
case Xml.TEXT:
..... // handle text within a tag
break;
case Xml.WHITESPACE:
..... // handle whitespace
break;
case Xml.COMMENT:
..... // handle comment
break;
case Xml.PROCESSING_INSTRUCTION:
..... // handle XML PI
break;
case Xml.DOCTYPE:
..... // handle XML doctype
break;
case Xml.END_DOCUMENT:
..... // end of document;
keepParsing = false;
break;
}
}
}
catch( java.io.IOException e ){
}
下面是一个J2ME的应用程序简单演示了如何解析xml。如果有时间可以写写复杂的测试程序。你可以从如下地址下载源代码:XMLTest. 里面包括了kxml和nanoxml的源代码,如果想得到最新的源代码请参考他们的官方网站,在本站提供了kxml的在线API
package com.ericgiguere.techtips;
import java.io.*;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import nanoxml.*;
import org.kxml.*;
import org.kxml.parser.*;
/**
* Simple MIDlet that demonstrates how an XML document can be
* parsed using kXML or NanoXML.
*/
public class XMLTest extends MIDlet {
// Our XML document -- normally this would be something you
// download.
private static String xmlDocument =
"<list><item>apple</item>" +
"<item>orange</item>" +
"<item>pear</item></list>";
private Display display;
private Command exitCommand = new Command( "Exit",
Command.EXIT, 1 );
public XMLTest(){
}
protected void destroyApp( boolean unconditional )
throws MIDletStateChangeException {
exitMIDlet();
}
protected void pauseApp(){
}
protected void startApp() throws MIDletStateChangeException {
if( display == null ){ // first time called...
initMIDlet();
}
}
private void initMIDlet(){
display = Display.getDisplay( this );
String [] items;
//items = parseUsingNanoXML( xmlDocument );
items = parseUsingkXML( xmlDocument );
display.setCurrent( new ItemList( items ) );
}
public void exitMIDlet(){
notifyDestroyed();
}
private String[] parseUsingkXML( String xml ){
try {
ByteArrayInputStream bin = new ByteArrayInputStream(
xml.getBytes() );
InputStreamReader in = new InputStreamReader( bin );
XmlParser parser = new XmlParser( in );
Vector items = new Vector();
parsekXMLItems( parser, items );
String[] tmp = new String[ items.size() ];
items.copyInto( tmp );
return tmp;
}
catch( IOException e ){
return new String[]{ e.toString() };
}
}
private void parsekXMLItems( XmlParser parser, Vector items )
throws IOException {
boolean inItem = false;
while( true ){
ParseEvent event = parser.read();
switch( event.getType() ){
case Xml.START_TAG:
if( event.getName().equals( "item" ) ){
inItem = true;
}
break;
case Xml.END_TAG:
if( event.getName().equals( "item" ) ){
inItem = false;
}
break;
case Xml.TEXT:
if( inItem ){
items.addElement( event.getText() );
}
break;
case Xml.END_DOCUMENT:
return;
}
}
}
// Simple List UI component for displaying the list of
// items parsed from the XML document.
class ItemList extends List implements CommandListener {
ItemList( String[] list ){
super( "Items", IMPLICIT, list, null );
addCommand( exitCommand );
setCommandListener( this );
}
public void commandAction( Command c, Displayable d ){
if( c == exitCommand ){
exitMIDlet();
}
}
}
}