04-05-2012 02:36 AM
I have done XML Parsing and it is shown on the screen in a text format.Now I want to access that data and show it in an object choice field. In my Application there are continents and their respective citites and on the basis of selection of the continents the cities should be filtered and shown in another choice field.Kindly hep me........I m attaching the code with it...please help me out here.....thanks in advance.
package mypackage; import java.io.InputStream; import javax.microedition.lcdui.List; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import net.rim.device.api.ui.Field; import net.rim.device.api.ui.FieldChangeListener; import net.rim.device.api.ui.component.ObjectChoiceField; import net.rim.device.api.ui.component.RichTextField; import net.rim.device.api.ui.container.MainScreen; import net.rim.device.api.util.Arrays; import net.rim.device.api.xml.parsers.DocumentBuilder; import net.rim.device.api.xml.parsers.DocumentBuilderFactory; public final class MyScreen extends MainScreen implements FieldChangeListener { ObjectChoiceField basecity; ObjectChoiceField city; String region; List cities; String mCities[]; private static final String xmlFileName = "/xml/city.xml"; private static final int _tab = 4; Arrays arr; { setTitle("****************filtering cities***************"); // cities = new List(); try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputStream inputStream = getClass().getResourceAsStream( xmlFileName ); Document document = builder.parse( inputStream ); Element rootElement = document.getDocumentElement(); rootElement.normalize(); displayNode( rootElement, 0 ); } catch ( Exception e ) { System.out.println( e.toString() ); } basecity = new ObjectChoiceField("Select Region:",new String[] {"Asia","Europe","North America"}); add(basecity); basecity.setChangeListener(this); city = new ObjectChoiceField("Select city:",new String[]{}); add(city); } private void displayNode( Node node, int depth ) { if ( node.getNodeType() == Node.ELEMENT_NODE ) { StringBuffer buffer = new StringBuffer(); indentStringBuffer( buffer, depth ); NodeList childNodes = node.getChildNodes(); int numChildren = childNodes.getLength(); Node firstChild = childNodes.item( 0 ); if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE ) { buffer.append( firstChild.getNodeValue() ); if(firstChild.getNodeName().toString() == "region"){ region=node.getNodeValue(); } if(buffer.toString().equals("city_name")){ if(region == "1"){ int count =mCities[0].length; System.out.println("tct : "+count); mCities[0][count]=node.getNodeValue().toString(); }else if(region == "2"){ int count =mCities[1].length; mCities[1][count]=node.getNodeValue().toString(); }else if(region == "3"){ int count =mCities[2].length; mCities[2][count]=node.getNodeValue().toString(); } //arr.add(0, ) int ct = mCities.length; mCities[ct]=buffer.toString(); add( new RichTextField( buffer.toString() ) ); } } else { buffer.append( node.getNodeName() ); add( new RichTextField( buffer.toString() ) ); for ( int i = 0; i < numChildren; ++i ) { displayNode( childNodes.item( i ), depth + 1 ); } } } else { String nodeValue = node.getNodeValue(); if ( nodeValue.trim().length() != 0 ) { StringBuffer buffer = new StringBuffer(); indentStringBuffer( buffer, depth ); buffer.append( nodeValue ); if(node.getNodeName().toString() == "region"){ region = node.getNodeValue(); } if(buffer.toString().equals("city_name")){ if(region == "1"){ int count =mCities[0].length; System.out.println("tct : "+count); mCities[0][count]=node.getNodeValue().toString(); }else if(region == "2"){ int count =mCities[1].length; mCities[1][count]=node.getNodeValue().toString(); }else if(region == "3"){ int count =mCities[2].length; mCities[2][count]=node.getNodeValue().toString(); } } int ct = mCities.length; mCities[ct]=buffer.toString(); add( new RichTextField( buffer.toString() ) ); } } } private static void indentStringBuffer( StringBuffer buffer, int depth ) { int indent = depth * _tab; for ( int i = 0; i < indent; ++i ) { buffer.append( ' ' ); } } public void fieldChanged(Field field, int context){ if(field == basecity){ String s = basecity.getChoice(basecity.getSelectedIndex()).to String(); if(s == "Asia"){ city.setChoices(mCities[0]); }else if (s == "Europe"){ city.setChoices(mCities[1]); }else if (s == "North America"){ city.setChoices(mCities[2]); } } } }
04-05-2012 02:44 AM
04-05-2012 04:47 AM
I donot have any idea of creating a data bean.Can u plz give me a demo or any other logical way to solve my problem???
04-05-2012 04:52 AM
04-06-2012 03:36 AM
i m using this code but it is showing error can u plz correct my code here........
i m just passing values here and i have made a diferent class"City.java" in which declairation is done.......but there is a problem in the class below.....may be in declairation of variables on top....or any where else....
import java.io.InputStream;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;
public final class Xmlparsing extends MainScreen {
public List city;//here i have declaired a city class in which i //have getters & setters
City citiesList;
private static final String xmlFileName = "/xml/city_list.xml";// assigning path of the xml file to the string
private static final int tab = 4;
public Xmlparsing() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream inputStream = getClass().getResourceAsStream( xmlFileName );
Document document = builder.parse(inputStream);
Element rootElement = document.getDocumentElement();
rootElement.normalize();
displayNode(rootElement, 0);
} catch (Exception e) {
System.out.println(e.toString());
}
}
private void displayNode( Node node, int depth )
{
if ( node.getNodeType() == Node.ELEMENT_NODE )
{
StringBuffer buffer = new StringBuffer();
indentStringBuffer( buffer, depth );
NodeList childNodes = node.getChildNodes();
int numChildren = childNodes.getLength();
Node firstChild = childNodes.item( 0 );
if ( numChildren == 1 && firstChild.getNodeType() == Node.TEXT_NODE )
{
buffer.append( node.getNodeName() ).append( " = \"" ).append( firstChild.getNodeValue() ).append( '"' );
add( new RichTextField( buffer.toString() ) );
}
else
{
buffer.append( node.getNodeName() );
add( new RichTextField( buffer.toString() ) );
// Recursively visit to all this node's children.
for ( int i = 0; i < numChildren; ++i )
{
displayNode( childNodes.item( i ), depth + 1 );
}
}
}
else
{
String nodeValue = node.getNodeValue();
if ( nodeValue.trim().length() != 0 )
{
StringBuffer buffer = new StringBuffer();
indentStringBuffer( buffer, depth );
buffer.append( '"' ).append( nodeValue ).append( '"' );
/////////////inserting values in getters & setters//////////////
city = new getLocalCityData()
{
citiesList = new ArrayList.cityfun();
try{
XmlPullParser xpp = getResources().getXml( xmlFileName );
while (xpp.getEventType()!=XmlPullParser.END_DOCUMENT) {
City city = new City();
if (xpp.getEventType()==XmlPullParser.START_TAG) {
if (xpp.getName().equals("city_id")) {
city.set_city_id(Integer.parseInt(xpp.getText()));
}
if (xpp.getName().equals("region_id")) {
city.set_region_id(Integer.parseInt(xpp.getText()) );
}
if (xpp.getName().equals("city_name")) {
city.set_city_name(xpp.getText());
}
if (xpp.getName().equals("is_dst")) {
city.set_is_dst(Integer.parseInt(xpp.getText()));
}
if (xpp.getName().equals("end_date")) {
city.set_end_date(xpp.getText());
}
if (xpp.getName().equals("dst_time_interval")) {
city.set_dst_time_interval(Integer.parseInt(xpp.ge tText()));
}//end of if(xpp)
CitiesList.add(city);
}//end of while
}catch(Exception e){
}//end of try catch
return CitiesList;
}//end of function localcitydata()
}//end of if(node value)
}// end of else
private static void indentStringBuffer(StringBuffer buffer, int depth) {
int indent = depth * tab;
for (int i = 0; i < indent; ++i) {
buffer.append(' ');
}
}
}