Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.39 KB | None | 0 0
  1. import android.app.ListActivity;
  2. import android.content.Intent;
  3. import android.os.AsyncTask;
  4. import android.support.v7.app.ActionBarActivity;
  5. import android.os.Bundle;
  6. import android.view.Menu;
  7. import android.view.MenuItem;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.ListAdapter;
  11. import android.widget.ListView;
  12. import android.widget.SimpleAdapter;
  13. import android.widget.TextView;
  14.  
  15. import org.w3c.dom.Document;
  16. import org.w3c.dom.Element;
  17. import org.w3c.dom.NodeList;
  18.  
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21.  
  22.  
  23. public class AndroidXMLParsingActivity extends ListActivity {
  24.  
  25. // All static variables
  26. static final String URL = "http://api.androidhive.info/pizza/?format=xml";
  27. // XML node keys
  28. static final String KEY_ITEM = "item"; // parent node
  29.  
  30. static final String KEY_ID = "id";
  31. static final String KEY_NAME = "name";
  32. static final String KEY_COST = "cost";
  33. static final String KEY_DESC = "description";
  34.  
  35. @Override
  36. public void onCreate(Bundle savedInstanceState) {
  37. super.onCreate(savedInstanceState);
  38. setContentView(R.layout.activity_android_xmlparsing);
  39.  
  40. new loadmore().execute();
  41.  
  42. /*// Selecting single ListView item
  43. ListView lv = (ListView) findViewById(R.id.list);
  44. lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  45.  
  46. @Override
  47. public void onItemClick(AdapterView parent, View view,int position, long id) {
  48. // Getting values from selected ListItem
  49. String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
  50. String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
  51. String description = ((TextView) view.findViewById(R.id.description)).getText().toString();
  52.  
  53. // Starting new intent
  54. Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
  55. in.putExtra(KEY_NAME, name);
  56. in.putExtra(KEY_COST, cost);
  57. in.putExtra(KEY_DESC, description);
  58. startActivity(in);
  59.  
  60. }
  61. });*/
  62. }
  63.  
  64. public class loadmore extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
  65.  
  66. ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
  67.  
  68. @Override
  69. protected ArrayList<HashMap<String, String>> doInBackground(
  70. String... params) {
  71. XMLParser parser = new XMLParser();
  72. String xml = parser.getXmlFromUrl(URL); // getting XML
  73. Document doc = parser.getDomElement(xml); // getting DOM element
  74.  
  75. NodeList nl = doc.getElementsByTagName(KEY_ITEM);
  76. // Looping through all item nodes <item>
  77. for (int i = 0; i < nl.getLength(); i++) {
  78. // Creating new HashMap
  79. HashMap<String, String> map = new HashMap<String, String>();
  80. Element e = (Element) nl.item(i);
  81. // Adding each child node to HashMap key => value
  82. map.put(KEY_ID, parser.getValue(e, KEY_ID));
  83. map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
  84. map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
  85. map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
  86.  
  87. // Adding HashList to ArrayList
  88. menuItems.add(map);
  89. }
  90.  
  91. return null;
  92. }
  93.  
  94. @Override
  95. protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
  96. // Adding menuItems to ListView
  97. ListAdapter adapter = new SimpleAdapter(AndroidXMLParsingActivity.this, menuItems,
  98. R.layout.list_item,
  99. new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
  100. R.id.name, R.id.description, R.id.cost });
  101.  
  102. setListAdapter(adapter);
  103. }
  104. }
  105.  
  106.  
  107. @Override
  108. public boolean onCreateOptionsMenu(Menu menu) {
  109. // Inflate the menu; this adds items to the action bar if it is present.
  110. getMenuInflater().inflate(R.menu.menu_android_xmlparsing, menu);
  111. return true;
  112. }
  113.  
  114. @Override
  115. public boolean onOptionsItemSelected(MenuItem item) {
  116. // Handle action bar item clicks here. The action bar will
  117. // automatically handle clicks on the Home/Up button, so long
  118. // as you specify a parent activity in AndroidManifest.xml.
  119. int id = item.getItemId();
  120.  
  121. //noinspection SimplifiableIfStatement
  122. if (id == R.id.action_settings) {
  123. return true;
  124. }
  125.  
  126. return super.onOptionsItemSelected(item);
  127. }
  128. }
  129.  
  130. import android.util.Log;
  131.  
  132. import org.apache.http.HttpEntity;
  133. import org.apache.http.HttpResponse;
  134. import org.apache.http.client.ClientProtocolException;
  135. import org.apache.http.client.methods.HttpPost;
  136. import org.apache.http.impl.client.DefaultHttpClient;
  137. import org.apache.http.util.EntityUtils;
  138. import org.w3c.dom.Document;
  139. import org.w3c.dom.Element;
  140. import org.w3c.dom.Node;
  141. import org.w3c.dom.NodeList;
  142. import org.xml.sax.InputSource;
  143. import org.xml.sax.SAXException;
  144.  
  145. import java.io.IOException;
  146. import java.io.StringReader;
  147. import java.io.UnsupportedEncodingException;
  148.  
  149. import javax.xml.parsers.DocumentBuilder;
  150. import javax.xml.parsers.DocumentBuilderFactory;
  151. import javax.xml.parsers.ParserConfigurationException;
  152.  
  153.  
  154. public class XMLParser {
  155.  
  156. public String getXmlFromUrl(String url) {
  157. String xml = null;
  158.  
  159. try {
  160. // defaultHttpClient
  161. DefaultHttpClient httpClient = new DefaultHttpClient();
  162. HttpPost httpPost = new HttpPost(url);
  163.  
  164. HttpResponse httpResponse = httpClient.execute(httpPost);
  165. HttpEntity httpEntity = httpResponse.getEntity();
  166. xml = EntityUtils.toString(httpEntity);
  167.  
  168. } catch (UnsupportedEncodingException e) {
  169. e.printStackTrace();
  170. } catch (ClientProtocolException e) {
  171. e.printStackTrace();
  172. } catch (IOException e) {
  173. e.printStackTrace();
  174. }
  175. // return XML
  176. return xml;
  177. }
  178.  
  179. public Document getDomElement(String xml){
  180. Document doc = null;
  181. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  182. try {
  183.  
  184. DocumentBuilder db = dbf.newDocumentBuilder();
  185.  
  186. InputSource is = new InputSource();
  187. is.setCharacterStream(new StringReader(xml));
  188. doc = db.parse(is);
  189.  
  190. } catch (ParserConfigurationException e) {
  191. Log.e("Error: ", e.getMessage());
  192. return null;
  193. } catch (SAXException e) {
  194. Log.e("Error: ", e.getMessage());
  195. return null;
  196. } catch (IOException e) {
  197. Log.e("Error: ", e.getMessage());
  198. return null;
  199. }
  200. // return DOM
  201. return doc;
  202. }
  203.  
  204. public String getValue(Element item, String str) {
  205. NodeList n = item.getElementsByTagName(str);
  206. return this.getElementValue(n.item(0));
  207. }
  208.  
  209. public final String getElementValue( Node elem ) {
  210. Node child;
  211. if( elem != null){
  212. if (elem.hasChildNodes()){
  213. for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
  214. if( child.getNodeType() == Node.TEXT_NODE ){
  215. return child.getNodeValue();
  216. }
  217. }
  218. }
  219. }
  220. return "";
  221. }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement