KXML1.21(STABLE)解析XML数据范例(J2ME客户端)

KXML1.21(STABLE)解析XML数据范例(J2ME客户端)_第1张图片


 XML文件如下:


<?xml version="1.0">
<root>
<child1>childData1</child1>
<child2>childData2</child2>
</root >

使用String作为XML数据源

注意我使用的事1.21版本的,和2.0版本的使用方式是不一样的


package com.zeph.j2me.xml;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import org.kxml.Xml;
import org.kxml.parser.ParseEvent;
import org.kxml.parser.XmlParser;

public class KXMLMIDlet extends MIDlet {

	private Display display;

	private Form form;

	private TextField textfield1;

	private TextField textfield2;

	public KXMLMIDlet() {

		display = Display.getDisplay(this);

		form = new Form("KXML1.21(Stable)");

		textfield1 = new TextField("Data", "", 10, TextField.ANY);

		textfield2 = new TextField("Data", "", 10, TextField.ANY);

		form.append(textfield1);

		form.append(textfield2);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

		display.setCurrent(form);

		xmlParse();

	}

	public void xmlParse() {

		String XML = "<?xml version=\"1.0\"?>" + "<root>" + "<child1>"
				+ "childData1" + "</child1>" + "<child2>" + "childData2"
				+ "</child2>" + "</root >";

		ByteArrayInputStream bis = new ByteArrayInputStream(XML.getBytes());

		InputStreamReader isr = new InputStreamReader(bis);

		XmlParser parser = null;

		try {

			parser = new XmlParser(isr);

		} catch (IOException e) {

			e.printStackTrace();

		}

		try {

			// 判断是否到达最后一个document标签
			boolean parsing = true;

			// 判断是否到达了我想要得到数据的Tag位置
			boolean isChildTag1 = false;

			// 判断是否到达了我想要得到数据的Tag位置
			boolean isChildTag2 = false;

			while (parsing) {

				ParseEvent event = parser.read();

				switch (event.getType()) {

				case Xml.START_DOCUMENT:

					System.out.println("start document");

					break;

				case Xml.START_TAG:

					System.out.println("start tag:" + event.getName());

					if (event.getName().equals("child1"))

						isChildTag1 = true;

					if (event.getName().equals("child2"))

						isChildTag2 = true;

					break;

				case Xml.TEXT:

					System.out.println("tag data:" + event.getText());

					if (isChildTag1)

						textfield1.setString(event.getText());

					if (isChildTag2)

						textfield2.setString(event.getText());

					break;

				case Xml.END_TAG:

					System.out.println("end tag:" + event.getName());

					if (event.getName().equals("child1"))

						isChildTag1 = false;
					
					if (event.getName().equals("child2"))

						isChildTag2 = false;

					break;

				case Xml.END_DOCUMENT:

					System.out.println("end document");

					parsing = false;

					break;

				}

			}

		} catch (IOException e) {

			e.printStackTrace();

		}

		parser = null;

		try {

			isr.close();

			bis.close();

		} catch (IOException e) {

			e.printStackTrace();

		}

	}
}


你可能感兴趣的:(xml,String,Class,j2me,Parsing)