i am trying to fetch xml data in listview,following is the i am using
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URL;
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 httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://itemsongs/assets/xml/itemsong_main.xml");
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;
}
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;
}
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 "";
}
static final String URL = "http://itemsongs/assets/xml/itemsong_main.xml";
static final String KEY_ITEM = "movie";
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl("http://itemsongs/assets/xml/itemsong_main.xml");
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName("movie");{
for (int i = 0; i < nl.getLength(); i++) {
String name = parser.getValue(e, "KEY_NAME");
String cost = parser.getValue(e, "KEY_COST");
String description = parser.getValue(e, "KEY_DESC");
}
}
}
i am using XMLParser code with the following code as MainActivity,though i am not getting any error but the code is not working..........please help ,i am completely lost...
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.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends ListActivity {
static final String URL = "http://ishirsoft.com/itemsongs/assets/xml/itemsong_main.xml";
static final String KEY_ITEM = "movie";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<hashmap><string,>> menuItems = new ArrayList<hashmap><string,>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl("http://ishirsoft.com/itemsongs/assets/xml/itemsong_main.xml");
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName("movie");
for (int i = 0; i < nl.getLength(); i++) {
HashMap<string,> map = new HashMap<string,>();
Element e = (Element) nl.item(i);
map.put("name", parser.getValue(e, "name"));
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.activity_main,
new String[] { KEY_ITEM }, new int[] {
R.id.listView1 });
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView int position, long id) {
Intent i= new Intent(MainActivity.this,XMLParser.class);
startActivity(i);
}
});
}
}