How to show Dom parser result in android list view - android

I would like to know how to implement the result of the following code into an android listview.
Thanks in advance
package com.androidpeople.xml.parsing;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class XMLParsingDOMExample extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];
try {
URL url = new URL(
"http://www.............com//example.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
website = new TextView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
website[i] = new TextView(this);
category[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("name");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("Name = "
+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("website");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
website[i].setText("Website = "
+ ((Node) websiteList.item(0)).getNodeValue());
category[i].setText("Website Category = "
+ websiteElement.getAttribute("category"));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
}

please guide me how i can put this code into android listview
Create a subclass of BaseAdapter that wraps around your parsed DOM and returns rows as requested via getView(). This is not significantly different than using ArrayAdapter for an ArrayList or CursorAdapter for a Cursor, except that you have to do more of the work yourself, since there is no DOMAdapter in Android.

Related

can we parse the Rss feed containg the link tag which contains image in html format

I am doing a Mobile Updates project an Android APP developing which needs to get the daily updates of new launched mobiles in market.
So for this i used the following link :http://techiedreams.com/android-simple-rss-reader/
By using the rss feed of link: (http://mobiles.sulekha.com/rss/latest-mobile-updates.htm)
and obtained the feeds from it the title and description also but could not get the image i am unable to understand how to parse it .
my Dom parser code as modified from the sample code:
package com.example.mobileupdates.parser;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
public class DOMParser {
private RSSFeed _feed = new RSSFeed();
public RSSFeed parseXml(String xml) {
URL url = null;
try {
url = new URL(xml);
} catch (MalformedURLException e1) {
e1.printStackTrace();
System.out.println(e1.getMessage());
}
try {
// Create required instances
DocumentBuilderFactory dbf;
dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Parse the xml
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
// Get all <item> tags.
NodeList nl = doc.getElementsByTagName("item");
int length = nl.getLength();
for (int i = 0; i < length; i++) {
Node currentNode = nl.item(i);
System.out.println(nl.getLength());
RSSItem _item = new RSSItem();
NodeList nchild = currentNode.getChildNodes();
int clength = nchild.getLength();
System.out.println("length" + clength);
System.out.println("nodes" + nchild);
// Get the required elements from each Item
for (int j = 0; j < clength; j = j + 1) {
Node thisNode = nchild.item(j);
String theString = null;
String nodeName = thisNode.getNodeName();
theString = nchild.item(j).getFirstChild().getNodeValue();
if (theString != null) {
if ("title".equals(nodeName)) {
// Node name is equals to 'title' so set the Node
// value to the Title in the RSSItem.
_item.setTitle(theString);
}
else if ("description".equals(nodeName)) {
_item.setDescription(theString);
// Parse the html description to get the image url
String html = theString;
org.jsoup.nodes.Document docHtml = Jsoup
.parse(html);
Elements imgEle = docHtml.select("img");
_item.setImage(imgEle.attr("src"));
}
else if ("pubDate".equals(nodeName)) {
// We replace the plus and zero's in the date with
// empty string
String formatedDate = theString.replace(" +0000",
"");
_item.setDate(formatedDate);
}
}
}
// add item to the list
_feed.addItem(_item);
}
} catch (Exception e) {
}
// Return the final feed once all the Items are added to the RSSFeed
// Object(_feed).
return _feed;
}
}
By changing this block of code i got title and description but no image obtained
// Get the required elements from each Item
for (int j = 0; j < clength; j = j + 1) {
please help me in obtaining it or can we have any other ways to do thi application without rss feeds

How to retrieve RSS feed? [duplicate]

This question already has answers here:
How to get rss feeds android?
(3 answers)
Closed 9 years ago.
I would like to retrieve the title and description from the link app2.nea.gov.sg/data/rss/nea_psi.xml. It's an RSS feed. How can I do this?
my code:
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
try {
URL url = new URL(
"http://app2.nea.gov.sg/data/rss/nea_psi.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
website = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
website[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = ((Document) fstElmnt).getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = ((Node) nameElement).getChildNodes();
name[i].setText("title = "
+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = ((Document) fstElmnt).getElementsByTagName("description");
Element websiteElement = (Element) websiteList.item(0);
websiteList = ((Node) websiteElement).getChildNodes();
website[i].setText("description = "
+ ((Node) websiteList.item(0)).getNodeValue());
layout.addView(name[i]);
layout.addView(website[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name ="#+id/result"
/>
</RelativeLayout>
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class XMLParsingDOMExample extends Activity {
ArrayList<String> title;
ArrayList<String> description;
public TextView title_text;
public TextView des_text;
//ItemAdapter adapter1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage_listitem_activity);
//ListView list = (ListView) findViewById(R.id.list);
title = new ArrayList<String>();
description = new ArrayList<String>();
title_text = (TextView) findViewById(R.id.title_text);
des_text = (TextView) findViewById(R.id.des_text);
try {
URL url = new URL(
"http://app2.nea.gov.sg/data/rss/nea_psi.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
title.add(""+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("description");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
description.add(""+ ((Node) websiteList.item(0)).getNodeValue());
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
title_text.setText(""+title.get(0));
String temp = Html.fromHtml(description.get(0)).toString();
String a[] = temp.split("\\)");
des_text.setText(""+a[0]+")");
}
mainpage_listitem_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:layout_margin="5dp"
android:textSize="22dp"
android:textColor="#FFFFFF"/>
<TextView
android:id="#+id/des_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="description "
android:layout_margin="5dp"
android:textSize="18dp"
android:textColor="#FFFFFF"/>
</LinearLayout>
Use the following library to fetch RSS feeds:
https://github.com/ahorn/android-rss

android xml parsing does not show results?

I am newbie to android, I am trying to parse xml data from http://www.astrology.com/horoscopes/daily-horoscope.rss, I grab the code from
How can I parse xml from url in android? here, I used sherLock library, I also set internet permissions in manifest, but still unable to grab it, here is the same code except a little modification,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/* Create a new textview array to display the results */
TextView name[];
//TextView website[];
//TextView category[];
try {
URL url = new URL("http://www.astrology.com/horoscopes/daily-horoscope.rss");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
// website = new TextView[nodeList.getLength()];
//category = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
//website[i] = new TextView(this);
//category[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = ((Document) fstElmnt).getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = ((Node) nameElement).getChildNodes();
name[i].setText("Name = " + ((Node) nameList.item(0)).getNodeValue());
//category[i].setText("Website Category = " + ((org.w3c.dom.Element) websiteElement).getAttribute("category"));
layout.addView(name[i]);
//layout.addView(website[i]);
//layout.addView(category[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
Remember I am extending my MainActivity class from SherlockActivity, when I execute the code, only action bar appears!

Android XML Parser isnt working

I am writing an android application with a XML parser.
I have a parser that used to work but when I run it it isnt doing anything.
This is my class:
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class XMLParsingUsingDomeActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
TextView ID[];
TextView vraag[];
TextView category[];
TextView a1[];
TextView p1[];
TextView a2[];
TextView p2[];
TextView a3[];
TextView p3[];
try {
URL url = new URL(
"http://128.140.217.126/vragen.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder dbu= dbf.newDocumentBuilder();
Document doc = dbu.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
ID = new TextView[nodeList.getLength()];
vraag = new TextView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];
a1 = new TextView[nodeList.getLength()];
p1 = new TextView[nodeList.getLength()];
a2 = new TextView[nodeList.getLength()];
p2 = new TextView[nodeList.getLength()];
a3 = new TextView[nodeList.getLength()];
p3 = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
ID[i] = new TextView(this);
vraag[i] = new TextView(this);
category[i] = new TextView(this);
a1[i] = new TextView(this);
p1[i] = new TextView(this);
a2[i] = new TextView(this);
p2[i] = new TextView(this);
a3[i] = new TextView(this);
p3[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("ID");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
ID[i].setText(((Node) nameList.item(0)).getNodeValue());
NodeList vraagList = fstElmnt.getElementsByTagName("vraag");
Element vraagElement = (Element) vraagList.item(0);
vraagList = vraagElement.getChildNodes();
vraag[i].setText(((Node) vraagList.item(0)).getNodeValue());
NodeList a1List = fstElmnt.getElementsByTagName("a1");
Element a1Element = (Element) a1List.item(0);
a1List = a1Element.getChildNodes();
a1[i].setText(((Node) a1List.item(0)).getNodeValue());
NodeList p1List = fstElmnt.getElementsByTagName("p1");
Element p1Element = (Element) p1List.item(0);
p1List = p1Element.getChildNodes();
p1[i].setText(((Node) p1List.item(0)).getNodeValue());
NodeList a2List = fstElmnt.getElementsByTagName("a2");
Element a2Element = (Element) a2List.item(0);
a2List = a2Element.getChildNodes();
a2[i].setText(((Node) a2List.item(0)).getNodeValue());
NodeList p2List = fstElmnt.getElementsByTagName("p2");
Element p2Element = (Element) p2List.item(0);
p2List = p2Element.getChildNodes();
p2[i].setText(((Node) p2List.item(0)).getNodeValue());
NodeList a3List = fstElmnt.getElementsByTagName("a3");
Element a3Element = (Element) a3List.item(0);
a3List = a3Element.getChildNodes();
a3[i].setText(((Node) a3List.item(0)).getNodeValue());
NodeList p3List = fstElmnt.getElementsByTagName("p3");
Element p3Element = (Element) p3List.item(0);
p3List = p3Element.getChildNodes();
p3[i].setText(((Node) p3List.item(0)).getNodeValue());
layout.addView(category[i]);
Toast.makeText(this,
"ID: " + i + "\n" +
"Vraag: " + ((Node) vraagList.item(0)).getNodeValue() + "\n" +
"A1: " + ((Node) a1List.item(0)).getNodeValue() + "\n" +
"P2: " + ((Node) p1List.item(0)).getNodeValue() + "\n" +
"A2: " + ((Node) a2List.item(0)).getNodeValue() + "\n" +
"P2: " + ((Node) p2List.item(0)).getNodeValue() + "\n" +
"A3: " + ((Node) a3List.item(0)).getNodeValue() + "\n" +
"P3: " + ((Node) p3List.item(0)).getNodeValue(),
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
}
And my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.pace.namace"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".XMLParsingUsingDomeActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
And the logcat output is worthless.
I didnt change the code but its just not working anymore.
What is the exception?
Couple of things can happen. It might be when you try to get URL from web service. Might be because you have to do your code in an AsyncTask.
I suggest you right click a line you suspect and Toggle Breakpoint, then go on your class in Package Explorer right-click it and go to "Debug As" then "Debug Configuration". Click Debug button after checking you're debugging the right project.
Welcome to debug mode, just try to see which line is causing the exception.

Android XML Parser not working but runs

I'm still not sure what the issue is... it runs but nothing is displayed. Ideas?
package com.androidpeople.xml.parsing;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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 android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
public class XMLParsingDOMExample extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView name[];
TextView website[];
TextView category[];
try {
URL url = new URL(
"http://www.androidpeople.com/wp-content/uploads/2010/06/example.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
/** Assign textview array lenght by arraylist size */
name = new TextView[nodeList.getLength()];
website = new TextView[nodeList.getLength()];
category = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
name[i] = new TextView(this);
website[i] = new TextView(this);
category[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("name");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
name[i].setText("Name = "
+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("website");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
website[i].setText("Website = "
+ ((Node) websiteList.item(0)).getNodeValue());
category[i].setText("Website Category = "
+ websiteElement.getAttribute("category"));
layout.addView(name[i]);
layout.addView(website[i]);
layout.addView(category[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
}
Use the permission in AndroidManifest.XML file:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Your code is working well.
The complete XML is:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidpeople.xml.parsing"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".XMLParsingDOMExample"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Categories

Resources