Advertisement
samadhanmedge

XMLParsing

Jan 10th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.65 KB | None | 0 0
  1. package com.androidhive.xmlparsing;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5.  
  6. import org.w3c.dom.Document;
  7. import org.w3c.dom.Element;
  8. import org.w3c.dom.NodeList;
  9.  
  10. import android.app.ListActivity;
  11. import android.content.Intent;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.view.View;
  15. import android.widget.AdapterView;
  16. import android.widget.AdapterView.OnItemClickListener;
  17. import android.widget.ListAdapter;
  18. import android.widget.ListView;
  19. import android.widget.SimpleAdapter;
  20. import android.widget.TextView;
  21.  
  22. public class AndroidXMLParsingActivity extends ListActivity {
  23.  
  24.     // All static variables
  25.     static final String URL = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%28select%20woeid%20from%20geo.places%20where%20text%3D%22Delhi%22%29&diagnostics=true";
  26.     // XML node keys
  27.     static final String KEY_ITEM = "item"; // parent node
  28.     static final String KEY_ID = "title";
  29.     static final String KEY_NAME = "geo:lat";
  30.     static final String KEY_COST = "geo:long";
  31.     static final String KEY_DESC = "yweather:forecast";
  32.  
  33.     @Override
  34.     public void onCreate(Bundle savedInstanceState) {
  35.         super.onCreate(savedInstanceState);
  36.         setContentView(R.layout.main);
  37.  
  38.         ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
  39.  
  40.         XMLParser parser = new XMLParser();
  41.         String xml = parser.getXmlFromUrl(URL); // getting XML
  42.         Log.d("xml",xml);
  43.        
  44.         Document doc = parser.getDomElement(xml); // getting DOM element
  45.         Log.d("doc",""+doc);
  46.         Log.d("parser.getDomElement(xml)",""+parser.getDomElement(xml));
  47.  
  48.         NodeList nl = doc.getElementsByTagName(KEY_ITEM);
  49.         Log.d("parser.getElementValue(doc);",""+parser.getElementValue(doc));
  50.        
  51.         parser.getElementValue(doc);
  52.         // looping through all item nodes <item>
  53.         for (int i = 0; i < nl.getLength(); i++) {
  54.             // creating new HashMap
  55.             HashMap<String, String> map = new HashMap<String, String>();
  56.             Element e = (Element) nl.item(i);
  57.             // adding each child node to HashMap key => value
  58.             map.put(KEY_ID, parser.getValue(e, KEY_ID));
  59.             Log.d("KEY_ID",parser.getValue(e, KEY_ID));
  60.            
  61.             map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
  62.             Log.d("KEY_NAME",parser.getValue(e, KEY_NAME));
  63.            
  64.             map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
  65.             Log.d("KEY_COST",parser.getValue(e, KEY_COST));
  66.            
  67.             map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
  68.             Log.d("KEY_DESC",parser.getValue(e, KEY_DESC));
  69.            
  70.             Log.d("map",""+map);
  71.             // adding HashList to ArrayList
  72.             menuItems.add(map);
  73.             Log.d("menuItems",""+menuItems);
  74.         }
  75.  
  76.         // Adding menuItems to ListView
  77.         ListAdapter adapter = new SimpleAdapter(this, menuItems,
  78.                 R.layout.list_item,
  79.                 new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
  80.                         R.id.name, R.id.desciption, R.id.cost });
  81.  
  82.         setListAdapter(adapter);
  83.  
  84.         // selecting single ListView item
  85.         ListView lv = getListView();
  86.  
  87.         lv.setOnItemClickListener(new OnItemClickListener() {
  88.  
  89.             @Override
  90.             public void onItemClick(AdapterView<?> parent, View view,
  91.                     int position, long id) {
  92.                 // getting values from selected ListItem
  93.                 String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
  94.                 String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
  95.                 String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
  96.                
  97.                 // Starting new intent
  98.                 Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
  99.                 in.putExtra(KEY_NAME, name);
  100.                 in.putExtra(KEY_COST, cost);
  101.                 in.putExtra(KEY_DESC, description);
  102.                 startActivity(in);
  103.  
  104.             }
  105.         });
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement