In the previous tutorial about Rss Reader Android app, I gave the example code about how to get the data from website using AsyncTask. After we get the data, we have to read it and convert the format to server our app. XML is the standard data format for RSS. In this android example, I will show you how to parse XML in Android. If you are first time to read this post, I think you also are interested in reading the these two examples:

Analyze Rss Feed Data Before Parsing in Android

First of all, let’s see what the rss feed data looks like. It is necessary before we start to parse the XML in Android. From the rss feed data, we will find which fields we will extract. Our parser in Android will get the data from those fields and ignores the rest. The following part is what we will parsed in our Rss Reader example. Each post to my website, jmsliu.com, is shown in the feed as an β€œitem” tag.

<item>
<title>
iPhone Rss Reader App IOS Tutorial 2: HTTP Network Programming in IOS
</title>
<link>https://jmsliu.com/1123/iphone-rss-reader-app-ios-tutorial-2-http-network-programming-in-ios.html</link>
<comments>https://jmsliu.com/1123/iphone-rss-reader-app-ios-tutorial-2-http-network-programming-in-ios.html#comments</comments>
<pubDate>Sat, 15 Dec 2012 11:12:03 +0000</pubDate>
<dc:creator>James</dc:creator>
<category>
<!&#91;CDATA&#91; iPhone iPad Object-C &#93;&#93;>
</category>
<category>
<!&#91;CDATA&#91; IOS Programming &#93;&#93;>
</category>
<guid isPermaLink="false">https://jmsliu.com/?p=1123</guid>
<description>
<!&#91;CDATA&#91;
To create a complete rss reader iphone app, http request and response are both necessary modules. Website, for example, WordPress powered website, usually provides rss feed feature. Hence, using our rss reader iphone app can easily get the rss feed contents from Http request. HTTP network programming in IOS is very simple. NSURLConnection class and &#91;...&#93;
&#93;&#93;>
</description>
<wfw:commentRss>https://jmsliu.com/1123/iphone-rss-reader-app-ios-tutorial-2-http-network-programming-in-ios.html/feed</wfw:commentRss>
<slash:comments>0</slash:comments>
</item>

Using ExpatPullParser to Parse XML in Android

To parse XML data format in Android, XmlPullParser is the recommended XML parse engine by Android developer site. It can parse XML in an efficient and maintainable way on Android. Besides this, we can also choose KXmlParser via XmlPullParserFactory.newPullParser() or ExpatPullParser via Xml.newPullParser(). Either xml parser can work well. In the Android developer site, it already give an example to show how to parse xml by ExpatPullParser. To give your more valuable Android example, I will use the recommended XML parser, XmlPullParser, to show you how to parse XML data in Android.

XmlPullParser makes the parsing job much easier. Generally, there are only several steps. Here is the simplified version:

XmlPullParserFactory factory = XmlPullParserFactory
		.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(is, null);

int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
	if (eventType == XmlPullParser.START_DOCUMENT) {
            //parsing start
	} else if (eventType == XmlPullParser.START_TAG) {
            //find a starting tag
	} else if (eventType == XmlPullParser.END_TAG) {
            //find a ending tag
	} else if (eventType == XmlPullParser.TEXT) {
	    //find text between tag
	}

	eventType = xpp.next();
}

The following code is my RSS Reader Example code. It is the subclass of AsyncTask. If you are not familiar with AsyncTask, you can check this Android example, AsyncTask Multi-thread Android Example. Inside doInBackground function, I am doing two jobs:

  • Loading Rss Data via HttpURLConnection
  • Parse XML via XmlPullParser

In parsing XML part, I am using XmlPullParser extract the title, date and link of the post from XML data. You can pay attention on the code after the comment “parse xml after getting the data”. Let’s see the source code example.

	private class RssDataController extends AsyncTask<String, Integer, ArrayList<PostData>>{
		private RSSXMLTag currentTag;

		@Override
		protected ArrayList<PostData> doInBackground(String... params) {
			// TODO Auto-generated method stub
			String urlStr = params[0];
			InputStream is = null;
			ArrayList<PostData> postDataList = new ArrayList<PostData>();
			try {
				URL url = new URL(urlStr);
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				connection.setReadTimeout(10 * 1000);
				connection.setConnectTimeout(10 * 1000);
				connection.setRequestMethod("GET");
				connection.setDoInput(true);
				connection.connect();
				int response = connection.getResponseCode();
				Log.d("debug", "The response is: " + response);
				is = connection.getInputStream();

				// parse xml after getting the data
				XmlPullParserFactory factory = XmlPullParserFactory
						.newInstance();
				factory.setNamespaceAware(true);
				XmlPullParser xpp = factory.newPullParser();
				xpp.setInput(is, null);

				int eventType = xpp.getEventType();
				PostData pdData = null;
				SimpleDateFormat dateFormat = new SimpleDateFormat(
						"EEE, DD MMM yyyy HH:mm:ss");
				while (eventType != XmlPullParser.END_DOCUMENT) {
					if (eventType == XmlPullParser.START_DOCUMENT) {

					} else if (eventType == XmlPullParser.START_TAG) {
						if (xpp.getName().equals("item")) {
							pdData = new PostData();
							currentTag = RSSXMLTag.IGNORETAG;
						} else if (xpp.getName().equals("title")) {
							currentTag = RSSXMLTag.TITLE;
						} else if (xpp.getName().equals("link")) {
							currentTag = RSSXMLTag.LINK;
						} else if (xpp.getName().equals("pubDate")) {
							currentTag = RSSXMLTag.DATE;
						}
					} else if (eventType == XmlPullParser.END_TAG) {
						if (xpp.getName().equals("item")) {
							// format the data here, otherwise format data in
							// Adapter
							Date postDate = dateFormat.parse(pdData.postDate);
							pdData.postDate = dateFormat.format(postDate);
							postDataList.add(pdData);
						} else {
							currentTag = RSSXMLTag.IGNORETAG;
						}
					} else if (eventType == XmlPullParser.TEXT) {
						String content = xpp.getText();
						content = content.trim();
						Log.d("debug", content);
						if (pdData != null) {
							switch (currentTag) {
							case TITLE:
								if (content.length() != 0) {
									if (pdData.postTitle != null) {
										pdData.postTitle += content;
									} else {
										pdData.postTitle = content;
									}
								}
								break;
							case LINK:
								if (content.length() != 0) {
									if (pdData.postLink != null) {
										pdData.postLink += content;
									} else {
										pdData.postLink = content;
									}
								}
								break;
							case DATE:
								if (content.length() != 0) {
									if (pdData.postDate != null) {
										pdData.postDate += content;
									} else {
										pdData.postDate = content;
									}
								}
								break;
							default:
								break;
							}
						}
					}

					eventType = xpp.next();
				}
				Log.v("tst", String.valueOf((postDataList.size())));
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (XmlPullParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return postDataList;
		}

		@Override
		protected void onPostExecute(ArrayList<PostData> result) {
			// TODO Auto-generated method stub
			for (int i = 0; i < result.size(); i++) {//ignore this comment >
				listData.add(result.get(i));
			}

			postAdapter.notifyDataSetChanged();
		}
	}

After parsing the xml, an ArrayList will be returned. Inside onPostExecute function, I will update the data list with new data which is extraced from XML. As a result, posts on my website will be shown in the ListView.

website rss feed in ListView

It seems there are only 5 posts. That is because I set my website to return 5 post in Rss Feed for each request.

Next Tutorial Drag to Refresh in ListView

In most ListView based content apps, they are using pull to refresh feature to update the content. In android example Tutorial 4, I will show to add drag and pull to refresh feature in the ListView app.

Go to next tutorial: Drag to Refresh in ListView Android Example

Get The Full Source Code

You can get the full source code of this project. With the source code, you can do any changes and use the project in any of your projects.
Go To Tutorial 5 And Get The Source Code

Previous PostNext Post

92 Comments

  1. Can I see all source code of RssDataController.java.Eclipse says about a lot errors.

    Sorry my bad english

  2. Hi Vladyslav,

    This is the whole source code of RssDataController.java. This android tutorial example is showing you how to Parse XML in Android. I just give the main part of the source code. If you want to get the whole project, please keep reading the tutorial 4 and tutorial 5.

    Thanks

    1. RSSXMLTag is a enum type available. It is defined in MainActivity. Here is the definiation:

      private enum RSSXMLTag {
      TITLE, DATE, LINK, CONTENT, GUID, IGNORETAG;
      }

    1. Hi, Henry,

      Please check onPostExecute function. In this function, I will add the data in listData, which is a class variable. So it is visible in inner class. After that, I will call postAdapter.notifyDataSetChanged(). This will let adapter update the view basing on new listData.

      Thanks

  3. Thank you for your prompt response.

    As far as I know, “listData” has not an “add” method that can be implemented.

    Regarding postAdapter, where did you defined it?
    Your example does not work to me because of that.

    Any help is appreciated.

    1. Hi Henry,

      This the No.3 tutorials among the whole tutorial. You’d better to go through the whole serial tutorials. The source code I posted in the tutorial are working properly.

      In this example, the main purpose is demonstrating how to get data from internet and parse the xml in a separate the thread. And it doesn’t cover the whole project.

      My advance is you’d better keep reading the reset of tutorials.

      Thanks

    1. Hi Henry,

      How did you fix these lines?

      listData.add(result.get(i));
      postAdapter.notifyDataSetChanged();

      Please let me know.

      1. Hi Mujeeb:

        listData is a main class variable. Change the type to “ArrayList listData;”. It will solve your problem.

        Thanks

  4. Hi,

    I’m not able to find this class “RSSXMLTag”.

    code is showing me the errors on these lines.

    private RSSXMLTag currentTag;
    currentTag = RSSXMLTag.IGNORETAG;

    listData.add(result.get(i));
    postAdapter.notifyDataSetChanged();

    If followed all ur examples but couldn’t find these.
    Please help me out.

    Thanks.

    1. i’m able to fix RSSXMLTag issue but still not able to resolve these …

      listData.add(result.get(i));
      postAdapter.notifyDataSetChanged();

    2. This example is a partial code, which show you how to parse the xml. To get the whole project code, you have to go through all the tutorials.

      Thanks

  5. What is postAdapter here and where is it defined? I fixed everything else but could fix this. also i went through the next 2 tutorials but couldnt find it. Thanks

    1. Hi Prasad,

      postAdapter is a class variable to render the list item. In tutorial 1, it is implemented as local variable, ArrayAdapter itemAdapter.

      If you want to get the full project source, please check tutorial 5.

      Thanks
      James

  6. I bought your app and I would like to rss feed with images and text in a listview see that is possible.
    all type of rss is displayed??

    1. Hi Boukhsibi,

      Image is not specified in Rss 1.1 Standard. In my example, I remain the image space for customized needs. If you customize your rss to contain the image, you need to change the code to load your image link in your customized rss data.

      Thanks
      James

  7. Hi James,

    I am Beginner of android Developer.
    I like your Android Rss Reader Example 1 but i don’t understand this example 2 and 3. I don’t understand how to connect MainActivity ListView. Can i get sample project or sample code.
    Thank for support πŸ™‚

    1. Hello,

      In my example, I define it as inner private class. Inner class is a basic knowledge in Java.

      When you check the class souce code, you can define your url outside of the class. When you call this class to download your rss feed, you just instantiate a class object, and pass your url inside.

      Best Regards
      James

      1. I have pasted this in MainActivity. But then i got ListView And postAdpter error.
        I solved the listView error. But post Adapter cannot be resolved. I did not find any variable named “postAdapter”.
        So I changed it to “PostItemAdapter”, then it can work but it is giving error.. “You can not put nonStatic variable in Static class” Something like this only.. I forget the exact statement…

  8. Ohk.. i am able to solve all the errors. But when i run the app,it does not parse my feeds and shows the local feed. i.e. “Post 1 Title : Title”

    1. Hello,

      • 1st, please check if your app gets all the xml feed;
      • 2nd, please check if your xml tags are parsed properly. If there are missing something, you have to change the code accordingly;
      • 3rd, please check all your data are stored properly in your postDataList, and it will be passed to your onPostExecute function. In the end, all your data will be stored in listData.

      I help above instruction will be helpful.

      Best Regards
      James

      1. How can i check these.?
        I am little beginner in android developmet.
        If i send you a code of main activity, will you be able to tell me problem.?

        1. If you got this warning message, that means you didn’t load the data. Therefore, you will not get the rss.

  9. The tutorial was a great one until this one. The first 2 steps went like cream and I could easily implement them.
    I got stuck at this function

    @Override
    protected void onPostExecute(ArrayList result) {
    // TODO Auto-generated method stub
    for (int i = 0; i < result.size(); i++) {
    listData.add(result.get(i));
    }

    postAdapter.notifyDataSetChanged();
    }

    Can you please explain how to go through this? I am getting errors on these line like everyone else.
    "postAdapter" – where is it?
    and which "listData" are you talking about.

    Your tutorial was so good until this point. Please help further.

    1. Hello,

      postAdaper is the main class variable. In tutorial 1, I create it as an local variable. But in the tutorial, I have to declare it as an main class variable so that onPostExecute can access it.

      The same as listData, you have to change your listData type to ArrayList listData;

      Thanks

      1. Can you please tell some code that I need to change. I am unable to follow this way.
        And from where do we call the method onPostExecute?

        Where have you mentioned the feed URL, its so confusing.

        I have been through tutorial 4 and 5 as well…but that also does not solve the problem. Please help.

  10. Hi i check your code your list view is working fine but when click on list item just open your website and display particular item

    this is not way. i need display details view from rss feed. i think you just pass url of your site ?

    1. Hello,

      You can check the source code, the onItemClickListener. I pass two data to next activity:

      1. postInfo.putString(“content”, data.postContent);
      2. postInfo.putString(“link”, data.postLink);

      In the coming activity, PostViewActivity class, I provide two ways to show the content.
      1. show the content from rss
      2. show the content from the link

      By default, I commented the first option and just show the content from the link. If you like, you can do the visa versa

      Best Regards
      James

  11. Hi James! I have been trying to create this app for some time, but can’t quite get it right. I am new to Android and would be very glad if you helped me.

    After including RssDataController, how do I use it?
    How can I use it to get a PostData array and include it inside listView?

    1. First, you need to make sure the array type is :
      private PostData[] datas;

      Then, you can store PostData objects in the array. If you don’t know what’s PostData, please check tutorial 1.

  12. hy nice tutorial
    however i m not able to solve listData and postAdapter problem. it says that it cant resolver these symbols. please guide.
    thanks

  13. I have created string variable postlink in class PostData.java but still im finding error 76,77 and 79 line for pdData.postlink. could you please help me with this.

    1. I guess there are something wrong in other place. Once you add postlink variable in class PostData.java, the pdData.postlink will not have any problems.

  14. I am having error with the code on line 003

    private RSSXMLTag currentTag;

    could you please help me with this pls?

        1. RSSXMLTag is a enum type available. It is defined in MainActivity. Here is the definiation:

          private enum RSSXMLTag {
          TITLE, DATE, LINK, CONTENT, GUID, IGNORETAG;
          }

          1. It works, but I am still having problem with this line of code

            listData.add(result.get(i));

            in the RssDataController class,
            the compiler can’t find any function “add” under listData

          1. The source code has been tested over hundred times :). Please check your code carefully, you may miss thing. BTW, can you please paste your error message.

          2. even I also have the same problem.
            And we are very sure that ur code would be perfect, I just wanna know where we are wrong?

    1. RssDataController is the subclass of asynctask. As my example source code, put your code in RssDataController.

  15. Hello James ,
    i have followed your tutorial it’s very useful but i’m facing a problem now

    postDataList is always empty i tried to debug the code and i find out that this the block inside this ” else if (eventType == XmlPullParser.END_TAG)” doesn’t compile as the condition doesn’t match , so postDataList is always empty , any thoughts how can i make this works ?

    1. First, please check your xml. If the condition “eventType == XmlPullParser.END_TAG” is never triggered, that means your xml structure is broken.

    1. Hello,

      Can you please give me your error message? My example code is tested for thousands of times already πŸ˜›

  16. Hi James!

    Great tutorial! Thanks a lot for this. However, I’m having some problem with this line:

    postAdapter.notifyDataSetChanged();

    I get the following error: “postAdapter cannot be resolved”

    I looked through the code to look for postAdapter but all I could find was itemAdapter. When I replaced postAdapter with itemAdapter, I still get the same error. How can I fix this error?

    Thanks in advance!

    1. postAdaper is the main class variable. In tutorial 1, I create it as an local variable. But in the tutorial, I have to declare it as an main class variable so that onPostExecute can access it.

      The same as listData, you have to change your listData type to ArrayList listData;

      Thanks

  17. Your code is very very stupid. You can’t use such thing to parse XML from URL. because you need retrieve whole XML.

    connection.setReadTimeout(10 * 1000);
    connection.setConnectTimeout(10 * 1000);

  18. this tutorial was great until this code here.

    I have gone through all the codes in the comment section and I still get this error.

    Error:(25, 13) error: cannot find symbol class RSSXMLTag.
    How can I fix it?

    1. Hello,

      RSSXMLTag is a enum type available. Check this:

      private enum RSSXMLTag {
      TITLE, DATE, LINK, CONTENT, GUID, IGNORETAG;
      }

Leave a Reply

Your email address will not be published. Required fields are marked *