Create Sitemap Using Java

Create Sitemap Using Java Tutorial describes about creating sitemap using java

It will also support for google site map updations

Sitemap is an easy manner for a webmasters to notify search engine spiders about pages on their sites that are accessible for crawling and indexed.

Sitemap is a simple XML file that consists of list of Entries (URLs) of a website along with some other details about URL like URL last updated time, how often URL updated ( ie; change frequency of URL) and how vital it is ( ie; priority of URL)

so that by using these informations search engines spiders can able to crawl the site.  

Create Sitemap Using Java


If you are using maven, you have to add following dependency. otherwise download sitemapgen4j-1.1.2.jar directly from the below sitemapgen url


You need to download

  1. OpenJDK 8 / Corretto
  2. SitemapGen4j

sitemapgen4j-1.1.2.jar in classpath

Features Of SitemapGen4j

SitemapGen4j is a java sitemap generator library useful to generate XML sitemaps in Java programming language.

By using SitemapGen4j you can able to add any number of URL's, can get gzipped output, can set lastmodified option, can set priority option, can set changefreq, can configure the date format, can validate sitemap with XML Schema Definition (XSD)

SitemapGen4j Example

import java.util.Date;

import com.redfin.sitemapgenerator.ChangeFreq;
import com.redfin.sitemapgenerator.WebSitemapGenerator;
import com.redfin.sitemapgenerator.WebSitemapUrl;

// Java Code To Generate Sitemap

public class SitemapGeneratorExample {

public static void main(String[] args) throws MalformedURLException {
// If you need gzipped output
WebSitemapGenerator sitemapGenerator = WebSitemapGenerator
("", new File("C:\\sitemap"))

    WebSitemapUrl sitemapUrl =
new WebSitemapUrl.Options(
.lastMod(new Date()).priority(1.0)
// this will configure the URL with lastmod=now, priority=1.0,
    // changefreq=hourly

    // You can add any number of urls here
Create Sitemap Using Java

SitemapGen4j with gzip

on above example just change the gzip parameter to true like below snippet, then code will generate gzipped sitemap

// If you need gzipped output
    WebSitemapGenerator sitemapGenerator = WebSitemapGenerator
        .builder("", new File("C:\\sitemap"))


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