title: SAX parser in BlackBerry
body: <p>My XML is in following format</p>
  
  <pre><code>&lt;users&gt;
    &lt;user uid="1" dispname ="Yogesh C" statid="1" statmsg = "Busy"&gt;Yogesh Chaudhari&lt;/user&gt;
    &lt;user uid="2" dispname ="Sameer S" statid="2" statmsg = "Available"&gt;Yogesh Chaudhari&lt;/user&gt;
  &lt;/users&gt;
  </code></pre>
  
  <p>In my BlackBerry app, I want to change the value of a particualar attribute, such as <code>statmsg</code> for uid 2.  Is it possible to do this using a SAX parser?  My aim is to update the XML and restore it.</p>
  
  <p>I used the following code for parsing my XML using SAX:</p>
  
  <pre><code>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+"==&gt;"+dispname+"==&gt;"+statid+"==&gt;"+statmsg);
                  LabelField lb = new LabelField("uid ==&gt;"+uid+"\ndispname==&gt;"+dispname+"\nstatid==&gt;"+statid+"\nstatmsg==&gt;"+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)
                  {
                  }
              }
          }
      }
  }
  </code></pre>