BlackBerry 中的 SAX 解析器

发布于 2024-10-09 05:43:40 字数 6918 浏览 0 评论 0原文

我的 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)
                {
                }
            }
        }
    }
}

My XML is in following format

<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>

In my BlackBerry app, I want to change the value of a particualar attribute, such as statmsg for uid 2. Is it possible to do this using a SAX parser? My aim is to update the XML and restore it.

I used the following code for parsing my XML using SAX:

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)
                {
                }
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文