BlackBerry中的SAX解析器

时间:2010-12-29 07:06:11

标签: blackberry java-me saxparser

我的XML采用以下格式

<users>
  <user uid="1" dispname ="Yogesh C" statid="1" statmsg = "Busy">Yogesh Chaudhari</user>
  <user uid="2" dispname ="Sameer S" statid="2" statmsg = "Available">Yogesh Chaudhari</user>
</users>

在我的BlackBerry应用程序中,我想更改某个特定属性的值,例如uid 2的statmsg。是否可以使用SAX解析器执行此操作?我的目标是更新XML并恢复它。

我使用以下代码使用SAX解析我的XML:

import java.io.InputStream;
import java.io.OutputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.file.FileConnection;

import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.MainScreen;

public class SaxParseUrl extends UiApplication {

    public SaxParseUrl() {

        pushScreen(new Pars());
    }

    public static void main(String[] args) {
        SaxParseUrl app = new SaxParseUrl();
        app.enterEventDispatcher();
    }
}

class Pars extends MainScreen implements ContentHandler 
{
    boolean name;

    Pars() {
        try {
            XMLReader parser = XMLReaderFactory.createXMLReader();
            parser.setContentHandler(this);
            FileConnection conn = (FileConnection)Connector.open("file:///store/home/user/employee.xml");
            InputStream is = conn.openDataInputStream();
            InputSource iss = new InputSource(is);
            parser.parse(iss);
        } catch (Exception e) {
            debug("file:///store/home/user/SAXParser.txt","Exception in pars() :"+e);
        }
    }

    public void startElement(String nsURI, String strippedName, String tagName,
            Attributes attributes) throws SAXException {
        try {
            debug("file:///store/home/user/SAXParser.txt","startElement");
            if (tagName.equalsIgnoreCase("user")) 
            {
                debug("file:///store/home/user/SAXParser.txt","Inside startElement");
                name = true;

                String uid = attributes.getValue("uid");
                String dispname = attributes.getValue("dispname");
                String statid = attributes.getValue("statid");
                String statmsg = attributes.getValue("statmsg");

                attributes.

                //LabelField lb = new LabelField(uid+"==>"+dispname+"==>"+statid+"==>"+statmsg);
                LabelField lb = new LabelField("uid ==>"+uid+"\ndispname==>"+dispname+"\nstatid==>"+statid+"\nstatmsg==>"+statmsg);
                add(lb);
            }
        } catch (Exception e) {
            System.out.println(e);

        }
    }

    public void characters(char[] ch, int start, int length) {
        debug("file:///store/home/user/SAXParser.txt","characters");
        if (name) {
            try {
                System.out.println("Title: " + new String(ch, start, length));

                    LabelField lb=new LabelField(""+ new String(ch, start, length));
                HorizontalFieldManager sr=new HorizontalFieldManager();
                sr.add(lb);
                add(sr);


                    //v_cid.addElement(new String(ch, start, length));
                //System.out.println("the elements of vector: " + v_cid);



                // name = false;
                //Enumeration e = v_cid.elements();
                //System.out.println("The elements of vector: " + v_cid);
                //while (e.hasMoreElements()) {
                    //System.out.println("The elements are: " + e.nextElement());
                //}*/
            } catch (Exception e) {
                System.out.println(e);
            }
        }

    }

    public void endDocument() throws SAXException {
        debug("file:///store/home/user/SAXParser.txt","endDocument");
    }

    public void endElement(String uri, String localName, String tagName)
            throws SAXException {
        debug("file:///store/home/user/SAXParser.txt","endElement");
    }

    public void endPrefixMapping(String prefix) throws SAXException {
        debug("file:///store/home/user/SAXParser.txt","endPrefixMapping");

    }

    public void ignorableWhitespace(char[] ch, int start, int length)
            throws SAXException {
        debug("file:///store/home/user/SAXParser.txt","ignorableWhitespace");

    }

    public void processingInstruction(String target, String data)
            throws SAXException {
        debug("file:///store/home/user/SAXParser.txt","processingInstruction");
    }

    public void setDocumentLocator(Locator locator) {
        debug("file:///store/home/user/SAXParser.txt","setDocumentLocator");
    }

    public void skippedEntity(String name) throws SAXException {

    }

    public void startDocument() throws SAXException {
        debug("file:///store/home/user/SAXParser.txt","startDocument");
    }

    public void startPrefixMapping(String prefix, String uri)
            throws SAXException {
        debug("file:///store/home/user/SAXParser.txt","startPrefixMapping");

    }

    public static void debug(String strFileName,String strData)
     {
        FileConnection fc = null;
        StringBuffer strbufData = null;
        OutputStream ops = null;

        try
        {   
            fc = (FileConnection)Connector.open(strFileName,Connector. READ_WRITE);

            //create the file if it doesnt exist
            if(!fc.exists())
            {
                fc.create();
            }

            InputStream is = fc.openDataInputStream();
            strbufData = new StringBuffer();
            int intCh;

            while((intCh = is.read())!= -1)
            {
                strbufData.append((char)intCh);
            }

            strbufData.append("\n\n"+strData);

            String strFileContent = new String(strbufData.toString());
            byte byteData[] = strFileContent.getBytes();

            ops = fc.openOutputStream();
            ops.write(byteData);
        }
        catch(Exception e)
        {
            Dialog.alert("Exception in writing logs :"+e);
        }
        finally
        {
            if(ops != null)
            {
                try
                {
                    ops.close();
                }catch(Exception e)
                {
                }
            }
            if(fc != null)
            {
                try
                {
                    fc.close();
                }catch(Exception e)
                {
                }
            }
        }
    }
}

0 个答案:

没有答案