scamexdotexe
scamexdotexe

Reputation: 159

Parsing xml data on Android

What I wanted to have is to collect all the results in an ArrayList and iterate in that arraylist and display the result but when I try to run the code below, it only return one element...

I have 3 classes...

public class XMLDataCollected {


     ArrayList<String> names = new ArrayList<String>();
     ArrayList<String> vicinities = new ArrayList<String>();
     ArrayList<String> types = new ArrayList<String>();
     ArrayList<String> lats = new ArrayList<String>();
     ArrayList<String> longs = new ArrayList<String>();


        String name = null;
        String vicinity = null;
        String type = null;
        String lat = null;
        String longi = null;


        public ArrayList<String> getName() {
            return names;
        }
        public void setName(String name) {
            this.names.add(name);
            this.name = name;
            Log.i("This is the name: ", name + "size: " + names.size());

        }
        public ArrayList<String> getVicinity() {
            return vicinities;
        }
        public void setVicinity(String vicinity) {
            this.vicinities.add(vicinity);
            this.vicinity = vicinity;           
            Log.i("This is the vicinity: ", vicinity);
        }
        public ArrayList<String> getType() {
            return types;
        }
        public void setType(String type) {
            this.types.add(type);
            this.type = type;
            Log.i("This is the type: ", type);
        }
        public ArrayList<String> getLat() {
            return lats;
        }
        public void setLat(String lat) {
            this.lats.add(lat);         
            this.lat = lat;
            Log.i("This is the latitude: ", lat);
        }
        public ArrayList<String> getLongi() {
            return longs;
        }
        public void setLongi(String longi) {
            this.longs.add(longi);          
            this.longi = longi;
            Log.i("This is the longitude: ", longi);
        }




        public String populate(){
            StringBuilder sb = new StringBuilder();
            sb.append(getName());
            sb.append(getVicinity());
            sb.append(getType());
            sb.append(getLat());
            sb.append(getLongi());      

            return sb.toString();
        }



2.

public class HandlingXMLStuff extends DefaultHandler {

    String elementValue = null;
    Boolean elementOn = false;
    XMLDataCollected data;


    public ArrayList<String> getData() {
        return data.getName();
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub

          elementOn = true;

            if (localName.equals("result"))         {
                data = new XMLDataCollected();
            } else if (localName.equals("CD")) {

            }
    }

      @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {

            elementOn = false;

            /**
             * Sets the values after retrieving the values from the XML tags
             * */
            if (localName.equalsIgnoreCase("name"))
                data.setName(elementValue);
            else if (localName.equalsIgnoreCase("vicinity"))
                data.setVicinity(elementValue);
            else if (localName.equalsIgnoreCase("type"))
                data.setType(elementValue);
            else if (localName.equalsIgnoreCase("lat"))
                data.setLat(elementValue);
            else if (localName.equalsIgnoreCase("lng"))
                data.setLongi(elementValue);

        }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub
        if (elementOn) {
            elementValue = new String(ch, start, length);
            elementOn = false;
        }
    }

}



3.



public class XMLParsingActivity extends Activity implements OnClickListener {

    static final String baseURL = "http://www.google.com/ig/api?weather=";
    static final String test = "";

     TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button bone = (Button) findViewById(R.id.btnone);

         tv = (TextView) findViewById(R.id.tvWeather);
        bone.setOnClickListener(this);


    }


    public String buildURL(int lat, int longi){
        String url = "";
        return url;
    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        try{
            URL website = new URL(test);


            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();          
            XMLReader xr = sp.getXMLReader();

            HandlingXMLStuff doingWork = new HandlingXMLStuff();
            XMLDataCollected xdc = new XMLDataCollected();
            xr.setContentHandler(doingWork);

            xr.parse(new InputSource(website.openStream()));


            StringBuilder sb = new StringBuilder();
            String name = null;




            for(int index = 0; index < doingWork.getData().size(); index++){
                sb.append(doingWork.getData().get(index));
                Log.i("test","" + index);
            }   


//          tv.setText("Name: " + name + "\n" + "Vicinity: " + vicinity + "\n" + "Type: " + type + "\n " 
//                  + "Latitude: " + lat + "\n " + "Longitude: " + longi
//                  );
            tv.setText("The size:  "  + sb);



        }catch(Exception ex){
            tv.setText("Error" + ex.toString());
        }

    }
}

This is the sample XML file...

 <result>
  <name>Zaaffran Restaurant - BBQ and GRILL, Darling Harbour</name>
  <vicinity>Harbourside Centre 10 Darling Drive, Darling Harbour, Sydney</vicinity>
  <type>restaurant</type>
  <type>food</type>
  <type>establishment</type>
  <geometry>
   <location>
    <lat>-33.8719830</lat>
    <lng>151.1990860</lng>
   </location>
  </geometry>
  <rating>3.9</rating>  
   </result>

What I want to achieve here is to iterate in the result but the result arraylist has only one element...please help me...

Upvotes: 0

Views: 850

Answers (1)

jmishra
jmishra

Reputation: 2081

This should help you http://p-xr.com/android-tutorial-how-to-parseread-xml-data-into-android-listview/

EDIT Also I recommend using the generic LIST <T> instead of ArrayList<T> as follows

      List<String> names = new ArrayList<String>();
      ....
      ....

for code optimization as the latter ArrayList<String> has memory occupancy. Its not a big issue but optimization is the fundamental step to check the magnitude of efficiency (with respect to time duration and memory) of a program.

Upvotes: 1

Related Questions