Google Sitemap.xml

From OpenCms Wiki
Jump to: navigation, search

The following code will recurse down a directory tree, generating a compatible sitemap.xml file. You can submit this to google, yahoo, and microsoft, and control how they index your files (including finding links to files that aren't available through text links the spider can read. The protocol names the resources and tells the spider the last time they were modified, improving search spider efficiency.

It respects a set of properties (which inherit, so you only need to set them at a folder level to change the elements within):

  • sitemap_hidden : Hides the resource from the sitemap
  • sitemap_priority : sets the priority field (1 by default if not otherwise set)
  • sitemap_change_frequency : how often the resource changes (weekly by default)
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="">
<%@ page session="false" %>
<%@ page import="java.util.*,org.opencms.jsp.*,org.opencms.file.*,java.text.DateFormat, java.text.SimpleDateFormat, org.opencms.main.*" %>
<%@ taglib prefix="cms" uri="" %>
<%@ taglib prefix="c" uri="" %>
protected String BASE_URL="";
public static SimpleDateFormat ISO8601FORMAT = new SimpleDateFormat("yyyy-MM-dd");
private String recurseTree(CmsObject cmso,CmsJspActionElement jsp, String path)  {
	StringBuffer sb = new StringBuffer();
	try {
		ArrayList files = (ArrayList) cmso.getFilesInFolder(path);
		for (Iterator i = files.iterator(); i.hasNext(); ) {
			CmsFile f = (CmsFile);	
			String thispath =;
			CmsProperty secret = cmso.readPropertyObject(f,"sitemap_hidden",true);
			CmsProperty changeFreqProperty = cmso.readPropertyObject(f,"sitemap_change_frequency",true);
			CmsProperty priorityProperty = cmso.readPropertyObject(f,"sitemap_priority",true);
			String changeFrequency = changeFreqProperty.getValue("weekly");
			String priority = priorityProperty.getValue("1");
			if (secret.getValue("false").equals("false")
				&& (thispath.endsWith("html")||thispath.endsWith("jsp")||thispath.endsWith("pdf"))) {		
				//DateFormat df = DateFormat.getDateInstance();
				String niceDate = ISO8601FORMAT.format(new Date(f.getDateLastModified()));
		ArrayList folders = (ArrayList) cmso.getSubFolders(path);
		for (Iterator j = folders.iterator(); j.hasNext(); ) {
			CmsFolder f = (CmsFolder);
			sb.append( recurseTree(cmso,jsp, cmso.getSitePath(f) ) );
	catch (CmsException cmsException) {
		sb.append("A CMS exception occurred: "+cmsException.toString());	
	return sb.toString();
}//the close / open delimiters are necessary
CmsJspActionElement cms = new org.opencms.jsp.CmsJspActionElement(pageContext, request, response);
CmsObject cmso = cms.getCmsObject();
String url ="opencms.url");
int lastSlash = url.indexOf("/",8);
BASE_URL = url.substring(0,lastSlash);
out.println (recurseTree(cmso,cms, "/"));
Personal tools