Search code examples
blackberryxml-parsingsax

Error in sax parsing Blackberry 7.1


I am trying to use SAX parser in blackberry and used this tutorial :

http://javarevisited.blogspot.com/2011/12/parse-read-xml-file-java-sax-parser.html#ixzz2OZJ6vB8O

and this is the code for my parsing class

import java.io.ByteArrayInputStream;

import java.io.InputStream;


import net.rim.device.api.xml.parsers.SAXParser;
import net.rim.device.api.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Samp extends DefaultHandler{

    String url = "http://ravee.clientarea.in/exhibitr/getData.php?db_table=events";
    TheVectorClass tcv ;

    boolean currentElement = false;
    String currentValue = "";

//   private Account acct;
     private String temp;


    public void parsingmethod()

     {     

        System.out.println("in parsingmethod");
         SAXParserFactory spfac = SAXParserFactory.newInstance();

         //Now use the parser factory to create a SAXParser object
         //Create an instance of this class; it defines all the handler methods
        Samp handler;    
        try {
            SAXParser sp = spfac.newSAXParser();


             handler = new Samp();


//           XMLReader = new 

             System.out.println("url is     "
                     + url );
             InputStream is  = new ByteArrayInputStream(url.getBytes());
             //Finally, tell the parser to parse the input and notify the handler
             System.out.println("value of is     " +is);
             System.out.println("value of is     " +handler);

//           System.out.println("            sp.parse(is, handler)                "+            
//          here in next line I get the error 
             sp.parse(is, handler);

        }  catch (Exception e) {
            // TODO Auto-generated catch block
            System.out.println("here in samp error 2");
            e.printStackTrace();
            System.out.println("e.gemessage  2  " +e.getMessage() );
        }



    //Read more: http://javarevisited.blogspot.com/2011/12/parse-read-xml-file-java-sax-parser.html#ixzz2OZJ6vB8O

     }



    /*
     * Every time the parser encounters the beginning of a new element,
     * it calls this method, which resets the string buffer
     */ 
    public void startElement(String uri, String localName,
                  String qName, Attributes attributes) throws SAXException {
           temp = "";

           System.out.println("reached");
           if (qName.equalsIgnoreCase("parameters")) {
//                  acct = new Account();
//                  acct.setType(attributes.getValue("type"));
                  System.out.println("val " +temp);

           }
    }

    /*
     * When the parser encounters the end of an element, it calls this method
     */
    public void endElement(String uri, String localName, String qName)
                  throws SAXException {

           if (qName.equalsIgnoreCase("nme")) {
                  // add it to the list
//                  accList.add(acct);

               System.out.println("val1 " +temp);

           }

           else if (qName.equalsIgnoreCase("descriptin")) {
//                  acct.setName(temp);

               System.out.println("val1 " +temp);
           }



}

and calling it in my screen class like

public final class HomeScreen extends MainScreen
{
    /**
    * Creates a new HomeScreen object
    */

    Samp smp;
    String elemtn;

    Connection coon;
    boolean currentElement = false;
    String currentValue = "";

    HomeScreen hm;

    //     Samp smp ;
    TheVectorClass tcv ;
    public HomeScreen()
    {
        // Set the displayed title of the screen
        setTitle("parsing  ");
        final TheVectorClass tcv ;

        //        hm = new HomeScreen();

        smp = new Samp();

        try {
            MainScreenUpdaterThread thread = new MainScreenUpdaterThread(this , smp);
            thread.start();

            System.out.println("before");
            //            smp.parsingmethod();
            System.out.println("after");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("e.get message   "+e.getMessage());
        }   
    }
}

and the error I am getting is

Expecting an element

How can I resolve this error ?


Solution

  • error was due to the way to make a input stream

    InputStream is  = new ByteArrayInputStream(url.getBytes());
    

    we use this way to make an input stream when the xml file is on the local, when the file is on the server we need to open the connection so for that we open the connection like this

    first convert the string of url to streamconnection

    StreamConnection stream = (StreamConnection)Connector.open(url);
    
    and then open the input stream 
    
    InputStream is =  stream.openInputStream();
    

    then use the input stream to parse the xml