Retrofit 2 – How to parse sitemap in android

Learn to parse sitemap in an android app using Retrofit 2 using xml parsing with simple xml converter dependency.

In this example, we will be reading and parsing the sitemap of this blog. A sample entry is given below:

<urlset xmlns="">

1. Dependency

To enable to parsing sitemap in android, we need at least two dependencies i.e. retrofit and converter-simplexml.

dependencies { 
    compile 'com.squareup.retrofit2:retrofit:2.6.1'
    compile 'com.squareup.retrofit2:converter-simplexml:2.6.1'

2. Sitemap model

Start by creating model for consuming the sitemap entry items.

import java.util.ArrayList;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name = "urlset")
public class SitemapResponse 
	@ElementList(name = "url", inline = true)
	private ArrayList<SitemapEntry> url;

	public String toString() {
		return "SitemapResponse [urlset=" + url + "]";
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

public class SitemapEntry 
	private String loc;
	private String lastmod;
	private String changefreq;
	private float priority;
	public String toString() {
		return "SitemapEntry [loc=" + loc + ", lastmod=" + lastmod + ", 
				changefreq=" + changefreq + ", priority=" + priority
				+ "]";

3. Sitemap service interface

Create service interface which will be invoked by retrofit to execute HTTP request. Notice the API URL is sitemap.xml.

import retrofit2.Call;
import retrofit2.http.GET;

public interface SitemapService 
	@GET("sitemap.xml") Call<SitemapResponse> getFeed();

4. Read sitemap example

Let’s create a retrofit instance and execute the sitemap request. Given example uses the async request example.


import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;

public class SitemapServiceDemo 
	private static final String BASE_URL = "";

	private static Retrofit.Builder builder = new Retrofit.Builder().baseUrl(BASE_URL)

	private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor()

	private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();

	public static void main(String[] args) throws IOException 

		Retrofit retrofit =;

		SitemapService SitemapService = retrofit.create(SitemapService.class);

		Call<SitemapResponse> callAsync = SitemapService.getFeed();

		callAsync.enqueue(new Callback<SitemapResponse>() {
			public void onResponse(Call<SitemapResponse> call, Response<SitemapResponse> response) {
				if (response.isSuccessful()) {
					SitemapResponse apiResponse = response.body();
					// API response
				} else {
					System.out.println("Request Error :: " + response.errorBody());

			public void onFailure(Call<SitemapResponse> call, Throwable t) {
				if (call.isCanceled()) {
					System.out.println("Call was cancelled forcefully");
				} else {
					System.out.println("Network Error :: " + t.getLocalizedMessage());

Program output.

SitemapResponse [urlset=
		[SitemapEntry [loc= 

Drop me your questions related to reading sitemap in android app using Retrofit 2.

Happy Learning !!

Leave a Reply

Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial