I'm developing an Android app in which I created an activity that shows a list of directions taken from Google Directions API.
The url used is something like this: http://maps.googleapis.com/maps/api/directions/xml?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false
And the result is something like this:
<DirectionsResponse>
<status>OK</status>
<route>
<summary>I-40 W</summary>
<leg>
<step>
<travel_mode>DRIVING</travel_mode>
<start_location>
<lat>41.8507300</lat>
<lng>-87.6512600</lng>
</start_location>
<end_location>
<lat>41.8525800</lat>
<lng>-87.6514100</lng>
</end_location>
<polyline>
<points>a~l~Fjk~uOwHJy#P</points>
</polyline>
<duration>
<value>19</value>
<text>1 min</text>
</duration>
<html_instructions>Head <b>north</b> on <b>S Morgan St</b> toward <b>W Cermak Rd</b></html_instructions>
<distance>
<value>207</value>
<text>0.1 mi</text>
</distance>
</step>
...
... additional steps of this leg
...
... additional legs of this route
<duration>
<value>74384</value>
<text>20 hours 40 mins</text>
</duration>
<distance>
<value>2137146</value>
<text>1,328 mi</text>
</distance>
<start_location>
<lat>35.4675602</lat>
<lng>-97.5164276</lng>
</start_location>
<end_location>
<lat>34.0522342</lat>
<lng>-118.2436849</lng>
</end_location>
<start_address>Oklahoma City, OK, USA</start_address>
<end_address>Los Angeles, CA, USA</end_address>
<copyrights>Map data ©2010 Google, Sanborn</copyrights>
<overview_polyline>
<points>a~l~Fjk~uOnzh#vlbBtc~#tsE`vnApw{A`dw#~w\|tNtqf#l{Yd_Fblh#rxo#b}#xxSfytAblk#xxaBeJxlcBb~t#zbh#jc|Bx}C`rv#rw|#rlhA~dVzeo#vrSnc}Axf]fjz#xfFbw~#dz{A~d{A|zOxbrBbdUvpo#`cFp~xBc`Hk#nurDznmFfwMbwz#bbl#lq~#loPpxq#bw_#v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg#|kHntyArpb#bijCk_Kv~eGyqTj_|#`uV`k|DcsNdwxAott#r}q#_gc#nu`CnvHx`k#dse#j|p#zpiAp|gEicy#`omFvaErfo#igQxnlApqGze~AsyRzrjAb__#ftyB}pIlo_BflmA~yQftNboWzoAlzp#mz`#|}_#fda#jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC</points>
</overview_polyline>
<optimized_waypoint_index>0</optimized_waypoint_index>
<optimized_waypoint_index>1</optimized_waypoint_index>
<bounds>
<southwest>
<lat>34.0523600</lat>
<lng>-118.2435600</lng>
</southwest>
<northeast>
<lat>41.8781100</lat>
<lng>-87.6297900</lng>
</northeast>
</bounds>
</route>
</DirectionsResponse>
In every item of the list I want to show the information contained in <html_instructions>, <text> child of <distance> and <text> child of <duration>.
I was able to obtain the value of <html_instructions> tag, using this parser:
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.util.Log;
public class XMLParser {
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
// return DOM
return doc;
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue(Node elem) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
}
How can I modify this parser in order to shows also the values of the other two tags?
I was able to fix that by adding another method in XMLParser class:
public String getSecondValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(1));
}
and here is the usage in the activity:
import java.util.ArrayList;
import java.util.HashMap;
import maps.XMLParser;
import org.apache.commons.lang3.StringEscapeUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class DirectionsActivity extends ListActivity {
String url = "";
XMLParser parser;
String xml = "";
ArrayList<HashMap<String, String>> menuItems;
// All static variables
static final String KEY_STEP = "step"; // parent node
static final String KEY_HTML_INSTRUCTIONS = "html_instructions";
static final String KEY_DURATION = "duration";
static final String KEY_DISTANCE = "distance";
static final String KEY_TEXT = "text";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = this.getIntent();
url = intent.getExtras().getString("url");
System.out.println("---------------" + url);
menuItems = new ArrayList<HashMap<String, String>>();
parser = new XMLParser();
DownloadTask downloadTask = new DownloadTask();
// Start downloading XML data from Google Directions API
downloadTask.execute(url);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String instruction = ((TextView) view.findViewById(R.id.instruction)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_HTML_INSTRUCTIONS, instruction);
startActivity(in);
}
});
}
/** A class to download data from Google Directions URL */
private class DownloadTask extends AsyncTask<String, Void, String>{
ListAdapter adapter;
private ProgressDialog pd;
#Override
protected void onPreExecute() {
pd = new ProgressDialog(DirectionsActivity.this);
pd.setTitle("Processing...");
pd.setMessage("Calcolo direzione in corso dalla posizione corrente...");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
// Downloading data in non-ui thread
#Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try{
// Fetching the data from web service
data = parser.getXmlFromUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
// Executes in UI thread, after the execution of
// doInBackground()
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Document doc = parser.getDomElement(result); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_STEP);
// looping through all item nodes <step>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
String escapedKeyHtmlInstructions = StringEscapeUtils.escapeHtml4(KEY_HTML_INSTRUCTIONS);
// KEY_HTML_INSTRUCTIONS.replaceAll("\\<.*?>", "");
map.put(KEY_HTML_INSTRUCTIONS, parser.getValue(e, escapedKeyHtmlInstructions));
map.put(KEY_DURATION, parser.getValue(e, KEY_TEXT));
map.put(KEY_DISTANCE, parser.getSecondValue(e, KEY_TEXT));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
adapter = new SimpleAdapter(DirectionsActivity.this, menuItems,R.layout.list_item,
new String[] { KEY_HTML_INSTRUCTIONS, KEY_DURATION, KEY_DISTANCE}, new int[] {
R.id.instruction, R.id.duration, R.id.distance});
setListAdapter(adapter);
pd.dismiss();
}
}
}
Related
I'm retrieving an XML from the Google Maps Directions API.
The result is something like this:
<DirectionsResponse>
<status>OK</status>
<route>
<summary>I-40 W</summary>
<leg>
<step>
<travel_mode>DRIVING</travel_mode>
<start_location>
<lat>41.8507300</lat>
<lng>-87.6512600</lng>
</start_location>
<end_location>
<lat>41.8525800</lat>
<lng>-87.6514100</lng>
</end_location>
<polyline>
<points>a~l~Fjk~uOwHJy#P</points>
</polyline>
<duration>
<value>19</value>
<text>1 min</text>
</duration>
<html_instructions>Head <b>north</b> on <b>S Morgan St</b> toward <b>W Cermak Rd</b></html_instructions>
<distance>
<value>207</value>
<text>0.1 mi</text>
</distance>
</step>
...
... additional steps of this leg
...
... additional legs of this route
<duration>
<value>74384</value>
<text>20 hours 40 mins</text>
</duration>
<distance>
<value>2137146</value>
<text>1,328 mi</text>
</distance>
<start_location>
<lat>35.4675602</lat>
<lng>-97.5164276</lng>
</start_location>
<end_location>
<lat>34.0522342</lat>
<lng>-118.2436849</lng>
</end_location>
<start_address>Oklahoma City, OK, USA</start_address>
<end_address>Los Angeles, CA, USA</end_address>
<copyrights>Map data ©2010 Google, Sanborn</copyrights>
<overview_polyline>
<points>a~l~Fjk~uOnzh#vlbBtc~#tsE`vnApw{A`dw#~w\|tNtqf#l{Yd_Fblh#rxo#b}#xxSfytAblk#xxaBeJxlcBb~t#zbh#jc|Bx}C`rv#rw|#rlhA~dVzeo#vrSnc}Axf]fjz#xfFbw~#dz{A~d{A|zOxbrBbdUvpo#`cFp~xBc`Hk#nurDznmFfwMbwz#bbl#lq~#loPpxq#bw_#v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg#|kHntyArpb#bijCk_Kv~eGyqTj_|#`uV`k|DcsNdwxAott#r}q#_gc#nu`CnvHx`k#dse#j|p#zpiAp|gEicy#`omFvaErfo#igQxnlApqGze~AsyRzrjAb__#ftyB}pIlo_BflmA~yQftNboWzoAlzp#mz`#|}_#fda#jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC</points>
</overview_polyline>
<optimized_waypoint_index>0</optimized_waypoint_index>
<optimized_waypoint_index>1</optimized_waypoint_index>
<bounds>
<southwest>
<lat>34.0523600</lat>
<lng>-118.2435600</lng>
</southwest>
<northeast>
<lat>41.8781100</lat>
<lng>-87.6297900</lng>
</northeast>
</bounds>
</route>
</DirectionsResponse>
I try to print the value of the tag , but it shows also the <b> or the <div>.
Thus I try to use the class StringEscapeUtils like this:
StringEscapeUtils.escapeHtml4(KEY_HTML_INSTRUCTIONS);
but it doesn't work.
I use this XML Parser:
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.util.Log;
public class XMLParser {
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
// return DOM
return doc;
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
public final String getElementValue(Node elem) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
}
And here is the activity that shows the information taken from the XML:
import maps.XMLParser;
import org.apache.commons.lang3.StringEscapeUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class DirectionsActivity extends ListActivity {
String url = "";
XMLParser parser;
String xml = "";
ArrayList<HashMap<String, String>> menuItems;
// All static variables
static final String KEY_STEP = "step"; // parent node
static final String KEY_HTML_INSTRUCTIONS = "html_instructions";
static final String KEY_DURATION = "duration";
static final String KEY_DISTANCE = "distance";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = this.getIntent();
url = intent.getExtras().getString("url");
System.out.println("---------------" + url);
menuItems = new ArrayList<HashMap<String, String>>();
parser = new XMLParser();
DownloadTask downloadTask = new DownloadTask();
// Start downloading json data from Google Directions API
downloadTask.execute(url);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.instruction)).getText().toString();
// String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
// String description = ((TextView) view.findViewById(R.id.description)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_HTML_INSTRUCTIONS, name);
// in.putExtra(KEY_COST, cost);
// in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
/** A class to download data from Google Directions URL */
private class DownloadTask extends AsyncTask<String, Void, String>{
ListAdapter adapter;
private ProgressDialog pd;
#Override
protected void onPreExecute() {
pd = new ProgressDialog(DirectionsActivity.this);
pd.setTitle("Processing...");
pd.setMessage("Calcolo direzione in corso dalla posizione corrente...");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
// Downloading data in non-ui thread
#Override
protected String doInBackground(String... url) {
// For storing data from web service
String data = "";
try{
// Fetching the data from web service
data = parser.getXmlFromUrl(url[0]);
}catch(Exception e){
Log.d("Background Task",e.toString());
}
return data;
}
// Executes in UI thread, after the execution of
// doInBackground()
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Document doc = parser.getDomElement(result); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_STEP);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
String escapeKeyHtmlInstructions = StringEscapeUtils.escapeXml(KEY_HTML_INSTRUCTIONS);
// KEY_HTML_INSTRUCTIONS.replaceAll("\\<.*?>", "");
map.put(KEY_HTML_INSTRUCTIONS, parser.getValue(e, escapeKeyHtmlInstructions));
// map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
// map.put(KEY_DISTANCE, "Rs." + parser.getValue(e, KEY_DISTANCE));
// map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
adapter = new SimpleAdapter(DirectionsActivity.this, menuItems,R.layout.list_item,
new String[] { KEY_HTML_INSTRUCTIONS}, new int[] {
R.id.instruction});
setListAdapter(adapter);
pd.dismiss();
}
}
}
How can I fix that? Is there other libraries I can use?
I was able to fix that by adding this code in the activity:
instructionValue = parser.getValue(e, KEY_HTML_INSTRUCTIONS);
instructionValueFixed = Html.fromHtml(instructionValue).toString();
map.put(KEY_HTML_INSTRUCTIONS, instructionValueFixed);
you should use XML parser for such type of data. As it is XML not HTML you can use any parser like DOM , SAX or any, that you can get the data properly and save each tag data separately..
I want to retrieve the image link inside the description tag, my tags are all working, here is the sample of description tag:
[http://news.instaforex.com/analytics/rss][1]
I want to retrieve the image link or to show the image. My question is how to that base on my code below?
mainactivity
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://news.instaforex.com/analytics/rss";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_PUBDATE = "pubDate";
static final String KEY_DESCRIPTION = "description";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<HashMap<String, Spanned>> menuItems = new ` ArrayList<HashMap<String, Spanned>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, Spanned> map = new HashMap<String, Spanned>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_TITLE, Html.fromHtml(parser.getValue(e, KEY_TITLE)));
map.put(KEY_PUBDATE, Html.fromHtml(parser.getValue(e, KEY_PUBDATE)));
String description = parser.getValue(e, KEY_DESCRIPTION);;
int index = description.indexOf("The material has been provided by InstaForex Company", 0);
description = description.substring(0, index-1);
map.put(KEY_DESCRIPTION, Html.fromHtml(description));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_TITLE, KEY_PUBDATE }, new int[] {
R.id.name, R.id.cost });
setListAdapter(adapter);
((BaseAdapter) adapter).notifyDataSetChanged();
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String title = menuItems.get(position).get(KEY_TITLE).toString();
String pubDate = menuItems.get(position).get(KEY_PUBDATE).toString();
String description= menuItems.get(position).get(KEY_DESCRIPTION).toString();
System.out.println("PubDate==>"+pubDate+"\n Description===>"+description);
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_TITLE, title);
in.putExtra(KEY_PUBDATE, pubDate);
in.putExtra(KEY_DESCRIPTION, description);
startActivity(in);
}
});
}
}
xmlparser
package com.androidhive.xmlparsing;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.text.Html;
import android.util.Log;
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
dbf.setNamespaceAware(true);
if (dbf.isNamespaceAware()==Boolean.TRUE) {
dbf.setNamespaceAware(Boolean.FALSE);
}
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
I'm trying to make an app that parses an RSS feed. I run my app but the fetching of data is very slow although the source link ( http://api.androidhive.info/music/music.xml ) is very fast.
I need my app to respond quickly.
My app code :
package com.sta.map;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
public class POSTS extends Activity {
static final String KEY_ARTIST = "artist";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
WbAdapter adapter;
static String URL = "http://api.androidhive.info/music/music.xml";
ProgressDialog pDialog;
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.posts);
new ParseXMLTask().execute();
}
class ParseXMLTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
// Showing progress dialog before sending http request
pDialog = new ProgressDialog(POSTS.this);
pDialog.setMessage("Please wait..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected String doInBackground(String... unused) {
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url); // getting XML from URL
return xml;
}
#Override
protected void onPostExecute(String result) {
pDialog.dismiss();
XMLParser parser = new XMLParser();
Document doc = parser.getDomElement(result); // getting DOM element
NodeList nl = doc.getElementsByTagName("song");
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
list=(ListView)findViewById(R.id.list);
adapter= new WbAdapter(POSTS.this, songsList);
list.setAdapter(adapter);
}
}
}
XMLParser class :
package com.sta.map;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.util.Log;
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
EDIT 1: In the manifest I found this code :
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
I remove this part android:targetSdkVersion="16" and the parsing become very fast
Why ??
I found that on large files the performance of the XMLParser is very poor and very memory hungry. Specifically if one node in xml is very large, like an encoded bitmap. Think about it it has to load the entire string into memory at one time. I switched to parsing the file myself since I was only looking for one specific section and reading the data using a BufferedInputStream to avoid the memory spike.
This may not be your problem, but know that for very large files the XMLParser is not usable.
The problem is, when i try to get value from xml below
<item>
<title>The Return of Toastmasters</title>
<link>http://www.younginnovations.com.np/blogs/anjan/2012/06/return-toastmasters</link>
<description><p>As the title implies, it was the <strong>return of the Toastmasters</strong> at YoungInnovations on Thursday, May 24, 2012. I said "return" because Toastmasters saw a long gap of more than 3 months. Why the delay? Well, we shifted to a new office building and it took us some time to adjust properly into the new place. However, we're all glad that we continued with the event even after a long gap. And rightfully so, the MC for the day, Bimal Maharjan, announced the theme for the meeting: Return.</p>
<p><a href="http://www.younginnovations.com.np/blogs/anjan/2012/06/return-toastmasters" target="_blank">read more</a></p></description>
<comments>http://www.younginnovations.com.np/blogs/anjan/2012/06/return-toastmasters#comments</comments>
<category domain="http://www.younginnovations.com.np/category/tags/toastmasters">Toastmasters</category>
<pubDate>Mon, 04 Jun 2012 07:28:33 +0000</pubDate>
<dc:creator>anjan</dc:creator>
<guid isPermaLink="false">151 at http://www.younginnovations.com.np</guid>
</item>
and My application code below
News.java
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class News extends ListActivity {
static final String URL = "http://www.younginnovations.com.np/blog/feed";
static final String KEY_ITEM = "item";
static final String KEY_TITLE = "title";
static final String KEY_DESC = "description";
static final String KEY_DATE = "pubDate";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
Log.e("Element", e.toString());
System.out.println(e);
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.news_list_item,
new String[] { KEY_TITLE, KEY_DESC, KEY_DATE }, new int[] {
R.id.name, R.id.desciption, R.id.cost });
setListAdapter(adapter);
}
}
and XMLParse.java
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.util.Log;
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* #param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* #param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* #param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* #param Element node
* #param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
but while getting value of <description> tag.. there is HTML tags... So on "<" is returned as value.. in rest other its perfectly fine. How can i get all the value, or in best case how can i get values excluding HTML tags..
WAiting for help,
Thanks in advance
Thanks everyone for your support.. But i solved it in different way...
I did some changes in XMLParser.java file...
public final String getElementValue( Node elem ) {
Node child;
String returnValue = "" ;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
// String returnValue = child.getNodeValue();
returnValue += child.getTextContent();
}
}
// Log.e("ReturnValue", Html.fromHtml(returnValue).toString());
return Html.fromHtml(returnValue).toString();
}
}
return "";
}
But still i have a problem. If there is picture in xml, that is returned as "obj", which i dont want to see..
I have been developing an Android RSS Reader Application and have had a problem with fetching HTML content from description node.
I tried to fetch the data from following XML.
<description>
<p><a href="http://in.news.yahoo.com/search-arizona-girl-6-turns-back-her-tucson-021230195.html">
<img src="http://l.yimg.com/bt/api/res/1.2/t8X__zdk6shihpWw0Nenfw--/YXBwaWQ9eW5ld3M7Zmk9ZmlsbDtoPTg2O3B4b2ZmPTUwO3B5b2ZmPTA7cT04NTt3PTEzMA--/http://media.zenfs.com/en_us/News/Reuters/2012-04-24T021230Z_1_CDEE83N064W00_RTROPTP_2_USA-MISSING-ARIZONA.JPG"
width="130" height="86" alt="Handout photo of Isabel Mercedes Celis" align="left" title="Handout photo of Isabel Mercedes Celis" border="0" />
</a>
TUCSON, Arizona (Reuters) - The search for a missing 6-year-old Arizona girl who a authorities said may have been snatched from her bedroom in Tucson entered its third day on Monday as search dogs shifted investigators' attention back to the child's home. The parents of first-grader Isabel Mercedes Celis told detectives she was last seen on Friday night when they tucked her into bed, and was found to have vanished when a family member entered her room the next morning to awaken her, police said. ...
</p>
<br clear="all"/>
</description>
it display blank when I fetch data.
I paste my whole code below..
I am fetching Rss from this url:
http://in.news.yahoo.com/rss/crime
RSSActivity.java
package com.satyampv.dsta;
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import android.app.ListActivity;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class AstroRssActivity extends ListActivity {
/** Called when the activity is first created. */
//static final String URL = "http://findyourfate.com/rss/dailyhoroscope-feed.asp?sign=Taurus";
//static final String URL = "http://my.horoscope.com/astrology/daily-horoscopes-rss.html";
static final String URL = "http://in.news.yahoo.com/rss/crime";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_DESC = "description";
static final String KEY_LINK = "link";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXMLFromURL(URL);// getting XML
Document doc = parser.getDocumentElement(xml);// getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
map.put(KEY_LINK,parser.getValue(e, KEY_LINK));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_TITLE, KEY_DESC, KEY_LINK }, new int[] {
R.id.name, R.id.desciption, R.id.link });
setListAdapter(adapter);
XMLParser.java
package com.satyampv.dsta;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import android.util.Log;
public class XMLParser {
public XMLParser() {
}
public String getXMLFromURL(String url) {
String xml = null;
try {
// Default HTTP Client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
// Getting xml Dom element #param xml String
public Document getDocumentElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error", e.getMessage());
return null;
}
return doc;
}
public String getValue(Element item,String str){
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
// Getting node Value
// #params ele element
public final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling())
{
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
}
if your description Node contains HTML tags, it does not contain any TEXT_NODE type child, which is why it returns empty.
Check what type are the child nodes in your description Node to see what to do of these.
Edit
One solution is using getTextContent ( http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Node.html#getTextContent%28%29 ) instead of searching for a Text node. Instead of:
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
you can simply go :
return elem.getTextContent();
Later, you can parse the HTML using Html.fromHtml() :
HashMap<String, Spanned> map = new HashMap<String, Spanned>();
and
map.put(KEY_DESC, Html.fromHtml(parser.getValue(e, KEY_DESC)));
(see http://developer.android.com/reference/android/text/Html.html#fromHtml%28java.lang.String%29 )
Edit2
And replace
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
with
ArrayList<HashMap<String, Spanned>> menuItems = new ArrayList<HashMap<String, Spanned>>();
Moreover, I think SimpleAdapter expects Strings for TextViews, so you may need a ViewBinder to put Spanned in the TextView:
SimpleAdapter adapter = new SimpleAdapter(...);
adapter.setViewBinder(new SimpleAdapter.ViewBinder() {
public boolean setViewValue(View view, Object data, String textRepresentation) {
if (data instanceof Spanned && view instanceof TextView) {
((TextView) view).setText((Spanned) data));
}
}
}
Check this example.Its a simple RSS reader and very easy to understand... http://droidapp.co.uk/?p=166
The best way to display HTML content in android is using Webview.So display the data inside description node using webview.It will display both the text and also images.Here is a sample code...
WebView Description;
Description = (WebView)findViewById(R.id.webView1);
Description.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
Description.getSettings().setJavaScriptEnabled(true);
Description.getSettings().setBuiltInZoomControls(true);
Bundle mybundle = getIntent().getExtras();
Integer NewsNumber = mybundle.getInt("number");
final String CurrentTitle = Arrays.Title[NewsNumber];
String CurrentDescription = Arrays.Description[NewsNumber];
NewsTitle.setText(CurrentTitle);
Description.loadDataWithBaseURL (null, "<html>"+CurrentDescription+"</html>", "text/html", "UTF-8",
null);