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

📄 kxmldemo_pull.java

📁 j2me手机制作
💻 JAVA
字号:
package com.talkweb.micp.icsp;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
import org.kxml.parser.*;
import org.kxml.*;

/**
 * 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_pull
    extends javax.microedition.midlet.MIDlet {

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

    /**
     * Creates an XML parser and parses a file in the local store looking
     * for address tags.  When it encounters such a tag it passes the
     * parser to another function.  If the end of document is reached
     * it exits.
     */
    public void beginParse() {
        XmlParser parser = null;

        try {
            // create the parser and pass it the input stream for a file
            // Here you could also pass it an HTTP input stream
            parser = new XmlParser(new InputStreamReader(this.getClass().
                getResourceAsStream(resfile_name)), 300);

            // this is the event object we'll use to test against
            ParseEvent event = null;

            // now begin parsing the address and read until we get the
            // END_DOCUMENT event.
            while ( (event = parser.read()).getType() != Xml.END_DOCUMENT) {

                // If it's a start tag, then test to see what kind it is
                if (event.getType() == Xml.START_TAG) {

                    // get the name and find out if it's "address"
                    String name = event.getName();
                    if (name != null && name.equals("body")) {
                        // Print notification to System.err
                        System.err.println("--[ ADDRESS ]-----------");

                        // it is an address tag, so pass the parser over
                        // to the function that will parse the address
                        parseAddressTag(parser);

                        // Print end notification to System.err
                        System.err.println("------------------------\n");
                    }

                    name = null;
                }

                event = null;
            }

        }
        catch (IOException ioe) {
            // report error
            System.err.println("XML Parsing Error: " + ioe);
            ioe.printStackTrace();
        }
        finally {
            // try to close, and ignore any exceptions
            try {
                parser = null;
            }
            catch (Exception ignored) {}
        }
    }

    /**
     * Parses the portion of the document between <address> tags.  Once
     * the end of that tag is reached control is returned to the calling
     * function.
     *
     * @throws IOException Exception thrown by XmlParser
     */
    public void parseAddressTag(XmlParser parser) throws IOException {
        ParseEvent event = null;

        while ( (event = parser.peek()).getType() != Xml.END_DOCUMENT) {
            String name = event.getName();
            int type = event.getType();
            if (type == Xml.END_TAG && name.equals("body")) {
                event = null;
                name = null;
                return;
            }
            event = parser.read();
            if (type != Xml.START_TAG) {
                event = null;
                continue;
            }
            StartTag stag = (StartTag) event;
            //stag.getc
            System.out.println(stag.getName()+ "-----" +stag.getParent() ) ;




            //getElementAttribute(stag, event);   // 元素属性的处理

            event = null;

        }
    }

    /**
     * 得到元素的属性
     * @param startTag StartTag
     * @param event ParseEvent
     */
    public void getElementAttribute(StartTag startTag, ParseEvent event){
        if (startTag.getAttributeCount() <= 0) {
            event = null;
            startTag = null;
            return ;
        }

        Attribute at = startTag.getAttribute(0);
        System.err.println(startTag.getName() + "// " +
                           at.getName() + ":" + at.getValue());

    }
    /**
     * 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 + -