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

Issue with SAX parsing.Parser not going to the 2nd tag

<?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;
       }
    }
}

 

 

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

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

Re: Issue with SAX parsing.Parser not going to the 2nd tag

Any help?

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Issue with SAX parsing.Parser not going to the 2nd tag

xml parsing should be done in a separate thread. i would suggest to build a data structure (vector, for example) with the parsed data, and add them to the UI later. (currently you have a mess of data and UI code)
if you want to add them on the fly you have to use invokelater.

startElement should be called for every h_box, i suggest that you debug your code to see where something goes wrong.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue with SAX parsing.Parser not going to the 2nd tag

startElement is calling  h_box everytime.I debugged and once the first h_box tag ends,its returning to the calling part instead of entering into the startElement of the 2nd h_box tag

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Issue with SAX parsing.Parser not going to the 2nd tag

what do you mean with "calling part"?
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue with SAX parsing.Parser not going to the 2nd tag

private void getData(InputStream is) {
		
		
		try {
			
			this.parseData(is);
		}finally {
			try {
				if (is != null)
					is.close();
			} catch (Exception e) {
			}
		}
	}

 This is  the method which initiates the parsing.I am calling this method from another method

 

public void XMLparse(){
		InputStream is = null;
	
			is = getClass().getResourceAsStream("sample.xml");
			getData(is);
			addItems();
	}

 So once the first <h_box> tag ends,the control comes back to the 

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

Re: Issue with SAX parsing.Parser not going to the 2nd tag

Instead of continuing with the 2nd (h_box) tag

Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Issue with SAX parsing.Parser not going to the 2nd tag

it looks as if you have an empty exception handling there, which is not a good idea if you are trying to find an error.

my bet would be that your parseData method throws an exception, this is caught in getData and your XMLparse continues as if nothing has happened.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 436
Registered: ‎06-22-2010
My Device: na
My Carrier: blackberry developer

Re: Issue with SAX parsing.Parser not going to the 2nd tag

yes u r right,i am getting exception in parseData()

 

public void parseData(InputStream in) {
		MainParser parsehandler = new MainParser();
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser sax;
		try {
			sax = factory.newSAXParser();
			sax.parse(in, parsehandler);
		} catch (ParserConfigurationException e) {
			System.out.println("Exception in parse data........");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in parse data........");
			e.printStackTrace();
		}
	}

 In the console i can see these messages

 

Exception in parse data........
No stack trace

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

Re: Issue with SAX parsing.Parser not going to the 2nd tag

So there must b somethning wrong in parsing,how can i know what is wrong and fix it