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

📄 kxmldemo_dom.java~1~

📁 使用kxml解析xml
💻 JAVA~1~
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import org.kxml.parser.*;
//import org.kxml.kdom.*;
import org.kxml.*;
import org.kxml2.kdom.Document;
import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;
import org.xmlpull.v1.*;



/**
 * A simple app to demonstrate pull parsing using kXML.  It opens
 * a file from the resource directory /res then passes the InputStream
 * to a kXML parser.  The application will then ask the parser for the
 * next event.  If the start tag of an <address> element is
 * encountered then control is passed to the parseAddress function
 * which will begin retrieving data and continue doing so until
 * an end tag for </address> is encountered.
 *
 * This demonstrates how easy it is to use a pull parser by
 * fragmenting node parsing between functions.
 *
 * @author Robert Cadena
 */
public class kXMLDemo_dom extends javax.microedition.midlet.MIDlet {

	/**
	 * Name of resource file we'll use later
	 */
	public static final String resfile_name = "/address_book.xml";

	/**
	 * Creates an XML parser and parses a file in the local store into a
	 * DOM tree.  Then we traverse the DOM and print out its contents.
	 */
	public void beginParse() {
		XmlParser parser = null;
		Document doc = new Document();

		try {
			// Read in the resource
			InputStream in = this.getClass().getResourceAsStream(resfile_name);
			// Turn it into a reader
			InputStreamReader isr = new InputStreamReader(in);
			// initialize the parser with it.
			parser = new XmlParser( isr );

			// Pass the parser to the document.  At this point the
			// entire resource is parsed and now resides in memory.
			doc.parse( parser );

			parser = null;
		} catch (IOException ioe) {
			// report error
			System.err.println("XML Parsing Error: " + ioe);
			ioe.printStackTrace();

			parser = null;
			doc = null;

			return;
		}

		// Now we get the root element which is "address_book"
		Element root = doc.getRootElement();
		// Since the <address> tags are children of <address_book>
		// the we get the children of root and see which one is
		// called <address>.  We can't just say root.getElement("address")
		// because it will return an exception if more than one element
		// with that name exists
		int child_count = root.getChildCount();

		for (int i = 0; i < child_count ; i++ ) {
			if (root.getType(i) != Xml.ELEMENT) {
				continue;
			}

			Element kid = root.getElement(i);

			// it's not an address? continue
			if (!kid.getName().equals("address")) {

				continue;
			}

			// it is an address, so let's print out the contents
			System.err.println("--[ ADDRESS ]--------------------");

			// traverse that portion of the tree looking for contents
			// and printing them out
			int address_item_count = kid.getChildCount();

			for (int j = 0; j < address_item_count ; j++) {
				// if it's not an element tag then skip it
				if (kid.getType(j) != Xml.ELEMENT) {
					continue;
				}

				Element item = kid.getElement(j);

				System.err.println( item.getName() + ": " + item.getText(0));
				item = null;
			}


			System.err.println("---------------------------------\n");

			kid = null;
		}
	}


	/**
	 * Starts the parser
	 *
	 * @see javax.microedition.midlet.MIDlet#startApp()
	 */
	protected void startApp() throws MIDletStateChangeException {
		beginParse();
	}

	/**
	 * @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
	 */
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	}

	/**
	 * @see javax.microedition.midlet.MIDlet#pauseApp()
	 */
	protected void pauseApp() {
	}


}

⌨️ 快捷键说明

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