Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer
Accepted Solution

XML parsing using SAX Parser

<?xml version="1.0" encoding="UTF-8"?>
<app id="TestApp">
<page id="home" > 
  <h_box id="h_box_1" width="100%" height="30%" bgcolor="#3399FF">
     <v_box id="v_box_1" width="50%" height="100%" bgcolor="#8258FA">
    <control id="article1_item1" background="images/article1_image2.jpg"
                class="image"
                height="40%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="90%" >
            </control>
	<control id="article1_item1" background="images/article1_image1.jpg"
                class="image"
                height="40%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="90%" >
            </control>		
		</v_box>	
	<v_box id="v_box_2" width="50%" height="100%" bgcolor="#04B4AE">
    <control id="article1_item1" background="images/article2_image1.jpg"
                class="image"
                height="40%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="90%" >
            </control>
	<control id="article1_item1" background="images/article2_image2.jpg"
                class="image"
                height="40%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="90%" >
            </control>		
		</v_box>		
</h_box>
<v_box id="v_box_4" width="100%" height="45%" bgimage="images/bg.png">		
	<control id="article1_item1" background="images/article_1_image_1.jpg"
                class="image"
                height="65%"
                marginleft="10%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="75%" >
            </control>
			
	<control
                id="top1"
                class="text"
                height="60%"
                marginleft="2%"
                marginright="2%"
                text="The Egyptian military says it has launched missile strikes from helicopters against suspected Islamist militants in the Sinai peninsula."
                textcolor="#ffffff"
		fontweight="italic" 
                fontsize="7px"
                width="98%" >
            </control>
	</v_box>	
		
<h_box id="v_box_3" width="100%" height="25%" bgcolor="#502F2F">		
	<control id="article1_item1" background="images/article3_image1.jpg"
                class="image"
                height="85%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="30%" >
            </control>
			
	<control id="article1_item1" background="images/article3_image2.jpg"
                class="image"
                height="85%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="30%" >
            </control>		
			
		<control id="article1_item1" background="images/article_2_image_3.jpg"
                class="image"
                height="85%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="30%" >
            </control>
			
	<control id="article1_item1" background="images/article_6_image_2.jpg"
                class="image"
                height="85%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="30%" >
            </control>		
	<control id="article1_item1" background="images/block_1_social_1.jpg"
                class="image"
                height="85%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="30%" >
            </control>
			
	<control id="article1_item1" background="images/article_7_image_3.jpg"
                class="image"
                height="85%"
                marginleft="2%"
                marginright="2%"
                margintop="2%"
                onclick="page1"
                width="30%" >
            </control>					
	</h_box>	
</page>
</app>

 This is my XML.I am able to parse it using DOM parser,bt i m new to SAX parsing.Can somebody help me to parse it using SAX parser

Highlighted
Developer
Posts: 172
Registered: ‎01-17-2011
My Device: 9500 series
My Carrier: Airtel

Re: XML parsing using SAX Parser

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("<tag name>"))
    {
        ht = null;
        ht = new Hashtable();
    }
    else if(name.equals("<tag Name>"))
    {

    }
    Key=name;
}

/**
* Gets called, whenever a closed tag is found.
*/
public void endElement(String uri, String localName, String name) throws SAXException
{
    if(name.equals("<tag name>"))
    {
        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.

 

 

 

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: XML parsing using SAX Parser

Thanx man

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: XML parsing using SAX Parser

<?xml version="1.0" encoding="UTF-8"?>
<app id="Home">
<page id="home" transition="left" width="100%" height="100%"> 
<h_box id="h_box_4" width="100%" height="50%" bgcolor="#143BFF">
	<control id="article1_item1"     background="images/article_2_image_1.jpg"
                class="image"
                height="70%"
                marginleft="10px"
                marginright="5px"
                margintop="5px"
                onclick="page1"
                width="70%" >
            </control>	
</h_box>
<h_box id="h_box_4" width="100%" height="50%" bgcolor="#D021A7">
	
	<control id="article1_item1" background="images/article_1_image_1.jpg"
                class="image"
                height="40%"
                marginleft="10px"
                marginright="5px"
                margintop="5px"
                onclick="page1"
                width="80%" >
            </control>
	
</h_box>
</page>
</app>

 This is my present XML.When the first <h_box> tag ends,it goes to the portion from where it is called instead of parsing the 2nd <h_box> tag.My code for parsing is below

 

package mypackage;

import java.util.Hashtable;
import java.util.Vector;

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

public class MainParser extends DefaultHandler 
{
private String Key="";
private  Hashtable ht= null;;
Vector vec=null ;
public String parentTag;
public String mainTag;
public MainParser()
{

}
/**
* 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 
{
	String key = null;
	ht = new Hashtable();
    vec = new Vector();
  
    for (int i = 0; i < attributes.getLength(); i++) {
		key = attributes.getLocalName(i);
		String val = attributes.getValue(i);
		ht.put(key, val);
	
	}
    if(!(name.equalsIgnoreCase("control"))){
    	  MyScreen.parentTag=Key;
    }
   //MyScreen.parentTag=Key;
    if(name.equals("page"))
    {
        MyScreen.getPageData(ht);
    }
    else if(name.equals("v_box"))
    {
    	if(MyScreen.parentTag.equalsIgnoreCase("page")){
    		mainTag="v_box";	
    	parentTag="v_box";
    	}
    	else {
    		MyScreen.parentTag=name;
    		parentTag=name;
    	}
        MyScreen.getData(ht);
        if(MyScreen.backgrndcolor!=null){
        	MyScreen.vfm = new CustomVerticalFieldManager(MyScreen.wd, MyScreen.ht,
        			MyScreen.color);
			}
			else {
				MyScreen.vfm = new CustomVerticalFieldManager(MyScreen.wd, MyScreen.ht,MyScreen.padding,
						MyScreen.backgroundImg);
			}
        MyScreen.backgrndcolor=null;
    }
    
    else if(name.equals("h_box"))
    {
    	if(  MyScreen.parentTag.equalsIgnoreCase("page")){
    		mainTag="h_box";	
    	parentTag="h_box";
    	}
    	else {
    		MyScreen.parentTag=name;
    		parentTag=name;
    	}
        MyScreen.getData(ht);
        if(MyScreen.backgrndcolor!=null){
        	MyScreen.hfm = new CustomHorizontalFieldManager(MyScreen.wd, MyScreen.ht,
        			MyScreen.color);
			}
			else {
				MyScreen.hfm = new CustomHorizontalFieldManager(MyScreen.wd, MyScreen.ht,MyScreen.padding,
						MyScreen.backgroundImg);
			}
        MyScreen.backgrndcolor=null;
    }
    
    if(name.equals("control"))
    {
    	//MyScreen.parentTag=parentTag;
        MyScreen.getControlData(ht);
    }
    
    Key=name;
}

/**
* Gets called, whenever a closed tag is found.
*/
public void endElement(String uri, String localName, String name) throws SAXException
{
   if(name.equals("h_box"))
    {
    	if(mainTag.equalsIgnoreCase("h_box")){
    		MyScreen.hfm.add(MyScreen.hfm);	
    	}
    	else {
    		MyScreen.vfm.add(MyScreen.hfm);		
    	}
    	    MyScreen.parentTag=mainTag;
    }
    else if(name.equals("v_box"))
    {
    	if(mainTag.equalsIgnoreCase("v_box")){
    		MyScreen.vfm.add(MyScreen.vfm);	
    	}
    	else {
    		MyScreen.hfm.add(MyScreen.vfm);		
    	}
    	    MyScreen.parentTag=mainTag;
    }
    else if(name.equals("control"))
       {
    	 //  MyScreen.parentTag=mainTag;
       }
    }
}

 Please help

Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: XML parsing using SAX Parser

can anyone tell me where i am doing wrong in the parsing

Developer
Posts: 172
Registered: ‎01-17-2011
My Device: 9500 series
My Carrier: Airtel

Re: XML parsing using SAX Parser

You have closed this thread .. So i will suggest you to create a new thread to ask your question.