XML parsing using SAX for Blackberry

前端 未结 1 1074
谎友^
谎友^ 2021-01-25 09:39

I want to parse following xml file:


    
mahesh 1234

相关标签:
1条回答
  • 2021-01-25 10:01

    You Can use Dom & Sax parsers for XML Parsing .

    There is a Code snippet for Calling Xml from HTTP request & than to parse it using Sax Parser.

    SAXParserImpl saxparser = new SAXParserImpl();
    ListParser receivedListHandler=new  ListParser();
    static DataInputStream din = null;
    public static String response;
    
    
        HttpConnection hc = null;
            OutputStream os;
               try
               {  
                   final String url ="<Enter URL for Xml Http Address>"+ InitClass.getConnectionString()+";ConnectionTimeout=25000";
    
    
                   hc = (HttpConnection)Connector.open(url);
    
                   os = hc.openOutputStream();
                   os.write(postDataBytes);
    
                   if (hc.getResponseCode() == 200)
                        din = hc.openDataInputStream();
                    else
                        response = "" + hc.getResponseCode();
                    saxparser.parse(din, receivedListHandler);
               }
               catch (Exception e) 
               {
    
               }
               finally 
               {
                  try 
                  {
                      if(din!=null)
                          din.close();
                      din = null;
                      if(hc!=null)
                          hc.close();
                      hc = null;
                  }
                  catch (Exception e) {   }
               }
    

    /* parser Class */

     public class ListParser extends DefaultHandler 
    {
    private String Key="";
    private  Hashtable ht=new Hashtable();
    vector vec = new Vector();
    public ListParser()
    {
    
    }
    /**
    * Gets called, whenever a Xml is start .
    */
    public void startDocument() throws SAXException 
    {
    
    } 
    /**
    * Gets called, whenever a Xml is End .
    */
    public void endDocument() throws SAXException 
    { 
    
    
    } 
    /**
    * Gets called, whenever a new tag is found.
    */
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException 
    {
        if(name.equals("address"))
        {
            ht = null;
            ht = new Hashtable();
        }
        else if(name.equals("login"))
        {
    
        }
        Key=name;
    }
    
    /**
    * Gets called, whenever a closed tag is found.
    */
    public void endElement(String uri, String localName, String name) throws SAXException
    {
        if(name.equals("address"))
        {
            vec.addElement(ht);
        }
    }
    public void characters(char[] ch, int start, int length) throws SAXException 
    {
        String element=new String(ch, start, length);
        ht.put(Key, element);
    }
    }
    

    It will parse your XML and Will provide you data in vector vec in hashtables for per XML tag.

    0 讨论(0)
提交回复
热议问题