Exercises Java with Examples-
Practice Exercises Java 4,5 5 302940

Sitemap creator (v2) - Practice Exercises Java


Lesson 12:

Additional libraries


Exercise 12.8:

Sitemap creator (v2)


Objetive:

A "sitemap" is a file that webmasters can use in order to tell Google the webpages that their site consists on, and get a better positioning in the search engine.

Its basic appearance is:


<?xml version="1.0" encoding="utf-8" ?>
<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://miweb.com/</loc>
        <lastmod>2013-03-18</lastmod>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc>https://miweb.com/apartado1</loc>
        <lastmod>2013-03-18</lastmod>
        <changefreq>daily</changefreq>
    </url>
</urlset>

You must create a program which will receive as parameters the name of a text file containing the URLs, the modification date and the frequency of changes:

sitemapCreator urls.txt 2011-11-18 weekly

The text file would contain the list of the names of the files to be indexed, each line in a different line:


https://www.practiceexercisesjava.com/> 
https://www.practiceexercisesjava.com/practice-exercises-java.aspx 

Source Code:


import java.util.*;
public class Main
{
	static void main(String[] param)
	{
		if (param.length != 3)
		{
			System.out.println("Error number of params.");
			return;
		}

		String file = param[0];
		String date = param[1];
		String frecuency = param[2];

		ArrayList ListUrls = GetUrls(file);

		CreateSiteMap(ListUrls, frecuency, date);
	}

	private static void CreateSiteMap(ArrayList listHtml, String frecuency, String lastUpdated)
	{
		try
		{
			java.io.FileWriter writer = new java.io.OutputStreamWriter(File.Create("sitemap.xml"));

			writer.write("" + System.lineSeparator());
			writer.write("" + System.lineSeparator());

			for (String html : listHtml)
			{
				writer.write("" + System.lineSeparator());
				writer.write("" + html + "" + System.lineSeparator());
				writer.write("" + lastUpdated + "" + System.lineSeparator());
				writer.write("" + frecuency + "" + System.lineSeparator());
				writer.write("" + System.lineSeparator());
			}

			writer.write("" + System.lineSeparator());

			writer.close();
		}
		catch (java.lang.Exception e)
		{
			System.out.println("Error writing sitemap.");
		}
	}

	private static ArrayList GetUrls(String nameFile)
	{
		try
		{
			java.io.FileReader reader = new java.io.InputStreamReader(File.OpenRead(nameFile));
			java.io.BufferedReader readerBufferedReader = new java.io.BufferedReader(reader);
			String line = "";
			ArrayList urls = new List();

			do
			{
				line = readerBufferedReader.readLine();

				if (line != null)
				{
					urls.add(line);
				}
			} while (line != null);

			reader.close();

			return urls;
		}
		catch (java.lang.Exception e)
		{
			System.out.println("Error reading file.");

			return null;
		}
	}
Exercisey 12.8

Share Now:


Google Ads





Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.