import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class SitemapGenerator { private static final Log LOG = LogFactory.getLog(SitemapGenerator.class); // Base URL of your site private static final String BASE_URL = "https://example.com/"; public static void main(String[] args) { File folder = new File("path/to/your/web/root"); // Local folder containing site files StringBuilder sitemap = new StringBuilder(); // XML header sitemap.append("\n"); sitemap.append("\n"); // Recursively scan folder addFilesToSitemap(folder, sitemap, folder.getAbsolutePath()); sitemap.append(""); // Save to file try (FileWriter writer = new FileWriter("sitemap.xml")) { writer.write(sitemap.toString()); LOG.info("Sitemap generated successfully."); } catch (IOException e) { LOG.error("Error writing sitemap.", e); } } private static void addFilesToSitemap(File folder, StringBuilder sitemap, String rootPath) { for (File file : folder.listFiles()) { if (file.isDirectory()) { addFilesToSitemap(file, sitemap, rootPath); } else if (file.getName().endsWith(".html")) { // Only HTML files String relativePath = file.getAbsolutePath().substring(rootPath.length() + 1); String url = BASE_URL + URLEncoder.encode(relativePath.replace("\\", "/"), StandardCharsets.UTF_8); sitemap.append(" \n"); sitemap.append(" ").append(url).append("\n"); sitemap.append(" ").append(new Date()).append("\n"); sitemap.append(" \n"); } } } }