Parsing a Sitemap using Retrofit2

Learn to parse a sitemap in an Android app using Retrofit2, which uses XML parsing with a simple XML converter dependency. Here’s a Retrofit sitemap reader example.

retrofit2

In this guide, we will learn how to parse a sitemap in an Android application using Retrofit2. A sitemap is essentially a structured XML file that provides information about the pages, their hierarchy, and their updates on a website.

1. Sitemap

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

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<url>
		<loc>https://howtodoinjava.com/retrofit2/retrofit-sync-async-calls/</loc>
		<lastmod>2019-08-25T22:22:39+00:00</lastmod>
		<changefreq>monthly</changefreq>
		<priority>0.8</priority>
	</url>
	...
	...
</urlset>

2. Dependencies

To parse a sitemap in an Android application, you need two essential dependencies:

  • Retrofit for handling HTTP requests.
  • Simple XML Converter for parsing XML responses.

Add the following dependencies to your build.gradle file:

dependencies {
    implementation 'com.squareup.retrofit2:retrofit:2.6.1'
    implementation 'com.squareup.retrofit2:converter-simplexml:2.6.1'
}
<dependency>
	<groupId>com.squareup.retrofit2</groupId>
	<artifactId>retrofit</artifactId>
	<version>2.6.1</version>
</dependency>
<dependency>
	<groupId>com.squareup.retrofit2</groupId>
	<artifactId>converter-simplexml</artifactId>
	<version>2.6.1</version>
</dependency>

3. Model

To parse the sitemap, we need to define data models that map to the structure of the XML file. These models will represent the <urlset> and <url> elements in the sitemap.

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

@Root(name = "urlset", strict = false)
public class SitemapResponse {

	@ElementList(name = "url", inline = true)
	private ArrayList<SitemapEntry> url;

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

@Root(name = "url", strict = false)
public class SitemapEntry {

	@Element(name = "loc")
	private String loc;

	@Element(name = "lastmod")
	private String lastmod;

	@Element(name = "changefreq")
	private String changefreq;

	@Element(name = "priority")
	private float priority;

	@Override
	public String toString() {
		return "SitemapEntry [loc=" + loc + ", lastmod=" + lastmod + 
		       ", changefreq=" + changefreq + ", priority=" + priority + "]";
	}
}

4. Sitemap Service Interface

We will now create a Retrofit service interface that defines the HTTP request for fetching the sitemap. The API endpoint for this example is /sitemap.xml.

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

public interface SitemapService {

	@GET("sitemap.xml")
	Call<SitemapResponse> getFeed();
}

5. Fetching and Parsing the Sitemap using Retrofit2

Finally, we will build a Retrofit instance, execute the HTTP request, and parse the sitemap using an asynchronous call.

import java.io.IOException;
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 = "https://howtodoinjava.com/";

	private static Retrofit.Builder builder = new Retrofit.Builder()
			.baseUrl(BASE_URL)
			.addConverterFactory(SimpleXmlConverterFactory.create());

	private static HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor()
			.setLevel(HttpLoggingInterceptor.Level.BODY);

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

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

		httpClient.addInterceptor(loggingInterceptor);
		builder.client(httpClient.build());

		Retrofit retrofit = builder.build();

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

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

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

			@Override
			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=https://howtodoinjava.com/retrofit2/retrofit-sync-async-calls/, 
	              lastmod=2019-08-25T22:22:39+00:00, 
	              changefreq=monthly, 
	              priority=0.8],
	...
	...
	...
]]

6. Summary

This guide demonstrates how to parse a sitemap in an Android application using Retrofit2 and Simple XML Converter. By defining proper models and using Retrofit’s seamless HTTP call mechanism, you can easily retrieve and process structured XML data, such as sitemaps in your Android apps.

Happy Learning !!

Weekly Newsletter

Stay Up-to-Date with Our Weekly Updates. Right into Your Inbox.

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
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.