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

Reply
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100
Accepted Solution

xml parsing using dom

[ Edited ]

hi all,

 am very new to mobile development my doubts might be silly to you all,

but i need yur help as am new to this platform.

 my requirement is ,i need to parse a xml as url using DOM parser and

 i need to parse the sectionname alone from xml and display it in a

 

listfield.when i select any of the item in listfield it should navigate to

 

next screen and parse and xml again and list the contents of that sectionname.

class VVFApplication extends UiApplication
{
MainScreen _screen= new MainScreen();
String _node,_element;
Connection _connectionthread;


public static void main(String arg[])
{
VVFApplication application = new VVFApplication();
application.enterEventDispatcher();
}

public VVFApplication()
{

_screen.setTitle("Vishwak Video Framework");
_screen.add(new RichTextField("LOADING"));
_screen.add(new SeparatorField());
pushScreen(_screen);
_connectionthread = new Connection();
_connectionthread.start();
}

public void updateField(String node, String element){

String title="SectionName";
_screen.add(new RichTextField("*"+element));
// _screen.add(_listElements.addElement("*"+element) );
if(node.equals(title)){
_screen.add(new SeparatorField());
}
}

private class Connection extends Thread
{
public Connection()
{
super();
}

public void run()
{

Document doc;
StreamConnection conn;
try{
conn=(StreamConnection)Connector.open;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance();
DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
doc = docBuilder.parse(conn.openInputStream());
doc.getDocumentElement ().normalize ();
NodeList list=doc.getElementsByTagName("SectionName");
_node=new String();
_element = new String();
Object obj = Application.getEventLock();
for (int i=0;i<list.getLength();i++)
{
synchronized(obj)
{
Node value=list.item(i).
getChildNodes().item(0);
_node=list.item(i).getNodeName();
_element=value.getNodeValue();
updateField(_node,_element);
}
}
}

catch (Exception e)
{
System.out.println(e.toString());
}
}
}
}

 now i need to have a selectable listfield which displays the sectionname in list.and wen i select the item from list it shld navigate to next screen to show its contents.

can anyone suggest me some ideas.thanks in advance

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: xml parsing using dom

[ Edited ]

First, instead of your connection thread directly updating the field, have it just build the array of SectionName nodes, store the array in a member variable, and then call some notification method to populate and display the ListField on the UI thread. When the user selects an item from the ListField, build the screen with the contents of the SectionName node corresponding to the current selection index.

 

By the way, calling docBuilder.isValidating() does absolutely nothing (other than waste a few instruction cycles). You probably want to call docBuilderFactory.setValidating(true).




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100

Re: xml parsing using dom

 public void updateField(String node, String element){ 

     String title="SectionName"; 
    _screen.add(new RichTextField(" . "+element)); 
         if(node.equals(title)){ 
    _screen.add(new SeparatorField()); 
} 
}

 

i need to add the code for listfield now .... but i don know wr to fix it properly .

to be exact , in the above code ,

 

in the update function i ve given as,

 

_screen.add(new RichTextField(" . "+element));

 

it l list as a richtextfield as per this code ,

i need to to channge it to listfield now ,wat changes do i need to make in the above code ,

 

can yu send me some codes or links to refer ..

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: xml parsing using dom

Well, I'm not sure exactly what look you're trying to achieve, but I'd do it by creating your screen with a single ObjectListField. (An ObjectListField is a ListField that does all the work of displaying a list of objects as simple text using toString().) Let's call the ObjectListField _list. Then your code could be:

 

public void updateField(String node, String element) {
String title="SectionName";
_list.insert(_list.getSize(), " . " + element);
if(node.equals(title)){
_list.insert(_list.getSize());
}
}

I'm not sure if that will handle your section name separators the way you want, but it's the best you can do, I think, with an ObjectListField. Once everything is working, then we can help you get the look you want.

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100

Re: xml parsing using dom

i tried it .. but no list is being displayed .. the screen is empty ..

 

anyother way ??

 

since i need to parse it using dom and display as a list am confused with the logic..

also i don understand the list concept ..

 

help me Ted ..

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: xml parsing using dom

If the screen is empty, then either it doesn't have any fields or the fields are not displaying anything. Show us the code for how you build the screen, how you populate the listfield, and anything else that might be relevant. The more (relevant) information you provide, the more likely it is that you will receive help.

 

Have you read the documentation for ListField? Also, RIM has several articles on listfield in the Fields category of the knowledge base. There's a tutorial at http://developerlife.com/tutorials/?p=898, and a web search for "blackberry listfield" will turn up lots of examples.

 

You need to extract from the dom the information you want to display and add it to some data structure (usually an array or vector) used by a ListFieldCallback registered with the ListField. If you use ObjectListField, it acts as its own ListFieldCallback and draws each item as a text line using toString(). Otherwise, you need to write your own ListFieldCallback, which is pretty easy unless you want fancy layout for each row.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100

Re: xml parsing using dom

can yu tell me wr am wrong here..?

i need to populate the sectionname

import javax.microedition.io.*;
import java.util.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

class VVFApplication extends UiApplication
{ 
     MainScreen _screen= new MainScreen(); 
     String _node,_element; 
     Connection _connectionthread;
     ListField _list = new ListField();
     ListCallback _callback = new ListCallback();



public static void main(String arg[]) 
{ 
	VVFApplication application = new VVFApplication(); 
    application.enterEventDispatcher(); 
} 

public VVFApplication() 
{   
	
    _screen.setTitle("Vishwak Video Framework");
    _screen.add(new RichTextField("LOADING")); 
    _screen.add(new SeparatorField()); 
    _list.setCallback(_callback);

     pushScreen(_screen); 
    _connectionthread = new Connection(); 
    _connectionthread.start();
} 
private class ListCallback implements ListFieldCallback {
	   private Vector listElements = new Vector();
	   public void drawListRow(ListField list, Graphics g, int index, int y, int w) {
	      String text = (String)listElements.elementAt(index);
	      g.drawText(text, 0, y, 0, w);
	   }
	   public Object get(ListField list, int index) {
	       return listElements.elementAt(index);
	   }
	   public int indexOfList(ListField list, String p, int s) {
	       //return listElements.getSelectedIndex();
	       return listElements.indexOf(p, s);
	   }
	   public int getPreferredWidth(ListField list) {
	      return Graphics.getScreenWidth();
	   }
	   public void insert(String toInsert, int index) {
	       listElements.insertElementAt(toInsert, index);
	   }
	   public void erase() {
	       listElements.removeAllElements();
	   }
	   
	}


public void updateField(String node, String element){ 

     String title="SectionName"; 
    _screen.add(new RichTextField("*"+element)); 
    
    _list.insert(index);
    _callback.insert(element, index);



   // _screen.add(_listElements.addElement("*"+element) );
     if(node.equals(title)){ 
    _screen.add(new SeparatorField()); 
} 
} 

private class Connection extends Thread
{ 
    public Connection()
    { 
       super(); 
    } 

public void run()
    { 

     Document doc; 
     StreamConnection conn; 
     try{ 
     conn=(StreamConnection)Connector.open("http://vvf.vishwak.com/api/search/category?type=list;deviceside=true");  
     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance(); 
     DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();  
     docBuilder.isValidating(); 
     doc = docBuilder.parse(conn.openInputStream()); 
     doc.getDocumentElement ().normalize (); 
     NodeList list=doc.getElementsByTagName("SectionName"); 
    _node=new String(); 
    _element = new String(); 
     Object obj = Application.getEventLock();
     for (int i=0;i<list.getLength();i++)
     {
     synchronized(obj)
     { 
     Node value=list.item(i). 
     getChildNodes().item(0); 
    _node=list.item(i).getNodeName(); 
    _element=value.getNodeValue(); 
     updateField(_node,_element); 
     } 
     }
}

catch (Exception e)
   { 
      System.out.println(e.toString()); 
   } 
}
}
}

 

 

to list... i don understand this listfield concept in adding populating items at runtime.

help me plz...

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: xml parsing using dom

  1. You don't seem to be adding _list to _screen anywhere. You have it commented out inside updateField (which is good--it doesn't belong there); I suggest adding it in the VVFApplication constructor.
  2. When you finish parsing all the XML, you should call _list.setSize(listElements.size()). A logical place to do it is inside the run() method of Connection. However, this call needs to be done on the event thread. You also need to force the screen to repaint. To do this, put this code:
    VVFApplication.this.invokeLater(new Runnable() {
    public void run() {
    _list.setSize(listElements.size());
    _screen.invalidate();
    }
    });

    at the end of the run() method of Connection

  3. Your indexOfList logic does not conform to the rquirements and will not work as expected. (Basically, it's just an expensive way of returning -1. I'd suggest just returning -1 until you have everything else working. Consult the API for exactly what it is supposed to do. Using Vector.indexOf does not do the job.)
  4. There's no reason to have a separate ListCallback class; just declare VVFApplication to implement ListFieldCallback and move the four necessary methods to the top level. Replace the line _callback.insert(element, index) in updateField with listElements.insertElement(element, index).
  5. Do not call _list.insert(); that is unnecessary when you are setting the size as in (2) above.



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Trusted Contributor
kirthiga
Posts: 204
Registered: ‎07-14-2010
My Device: 8100

Re: xml parsing using dom

[ Edited ]

i modified my code as per yur suggestion ..

but it puts an error at index in this line,

 

_screen.add(listElements.insertElementAt(element,index) );

 

 

import javax.microedition.io.*; 
import net.rim.device.api.ui.*; 
import net.rim.device.api.ui.component.*; 
import net.rim.device.api.ui.container.*; 
import net.rim.device.api.system.*; 
import net.rim.device.api.xml.parsers.*; 
import org.w3c.dom.*; 
import java.util.*;
//import org.xml.sax.*; 

class VVFApplication extends UiApplication implements ListFieldCallback 
{ 
     MainScreen _screen= new MainScreen(); 
     String _node,_element; 
     Connection _connectionthread; 
     ListField _list = new ListField();
     private Vector listElements = new Vector();


public static void main(String arg[])
{ 
	VVFApplication application = new VVFApplication(); 
    application.enterEventDispatcher(); 
} 

public VVFApplication() 
{ 
    _screen.setTitle("Vishwak Video Framework");
    _screen.add(new RichTextField("LOADING")); 
    _screen.add(new SeparatorField()); 
    ListFieldCallback _callback = null;
	_list.setCallback(_callback);
    _screen.add(_list);
     pushScreen(_screen); 
    _connectionthread = new Connection(); 
    _connectionthread.start();
} 

public void drawListRow(ListField list, Graphics g, int index,
		int y, int width) {
	String text = (String)listElements.elementAt(index);
    g.drawText(text, 0, y, 0, width);

	
	
}

public Object get(ListField list, int index) {
	// TODO Auto-generated method stub
	 return listElements.elementAt(index);

}

public int getPreferredWidth(ListField list) {
	// TODO Auto-generated method stub
	return Graphics.getScreenWidth();

}
public void insert(String toInsert, int index) {
    listElements.insertElementAt(toInsert, index);
}


public int indexOfList(ListField list, String prefix, int start) {
	// TODO Auto-generated method stub
	return -1;
}
public void updateField(String node, String element){ 

     String title="SectionName"; 
     //listElements.insertElementAt(element,index);
     _screen.add(listElements.insertElementAt(element,index) );

   // _screen.add(new RichTextField(" . "+element)); 
     if(node.equals(title)){ 
    _screen.add(new SeparatorField()); 
} 
} 

private class Connection extends Thread
{ 
    public Connection()
    { 
       super(); 
    } 

public void run()
    { 

     Document doc; 
     StreamConnection conn; 
     try{ 
     conn=(StreamConnection)Connector.open("http://...;deviceside=true");  
     DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance(); 
     DocumentBuilder docBuilder= docBuilderFactory.newDocumentBuilder();  
     docBuilder.isValidating(); 
     doc = docBuilder.parse(conn.openInputStream()); 
     doc.getDocumentElement ().normalize (); 
     NodeList list=doc.getElementsByTagName("SectionName"); 
    _node=new String(); 
    _element = new String(); 
     Object obj = Application.getEventLock();
     for (int i=0;i<list.getLength();i++)
     {
     synchronized(obj)
     { 
     Node value=list.item(i). 
     getChildNodes().item(0); 
    _node=list.item(i).getNodeName(); 
    _element=value.getNodeValue(); 
     updateField(_node,_element); 
     } 
     }
}

catch (Exception e)
   { 
      System.out.println(e.toString()); 
   } 
VVFApplication.this.invokeLater(new Runnable()
{    public void run() {     
	_list.setSize(listElements.size());   
	_screen.invalidate();    }
});
}
}





}

 

Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: xml parsing using dom

You should not add anything to _screen inside updateField. Change that line to:

listElements.insertElementAt(element, index);

 




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.