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

pom.xml

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

<dependency>
    <groupId>com.github.dfabulich</groupId>
    <artifactId>sitemapgen4j</artifactId>
    <version>1.1.2</version>
</dependency>

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.io.File;
import java.net.MalformedURLException;
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
        .builder
("http://www.javatips.net", new File("C:\\sitemap"))
       
.gzip(false).build();

    WebSitemapUrl sitemapUrl =
new WebSitemapUrl.Options(
       
"http://www.javatips.net/blog/2011/08/findbugs-in-eclipse-java-tutorial")
       
.lastMod(new Date()).priority(1.0)
       
.changeFreq(ChangeFreq.HOURLY).build();
   
// this will configure the URL with lastmod=now, priority=1.0,
    // changefreq=hourly

    // You can add any number of urls here
   
sitemapGenerator.addUrl(sitemapUrl);
    sitemapGenerator
        .addUrl
("http://www.javatips.net/blog/2011/09/create-sitemap-using-java");
    sitemapGenerator.write
();
 
}
}
output
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("http://www.javatips.net", new File("C:\\sitemap"))
        .gzip(true).build();

output












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