In this hands on, we are going to generate an XML sitemap that is formatted for use with Google Sitemaps.
Tags Used: <cfxml>, <cfoutput>, <cffile>
Functions Used: toString
- Create a new file in the /www/ folder called generateSitemap.cfm.
-
On line 1, create an open <cfxml> tag and provide the following attribute:
- variable: xmlSiteMap
-
After the opening <cfxml> tag enter the following code:
<?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.google.com/schemas/sitemap/0.90"> <url> <loc>http://www.myWebsite.com/</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/about.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/resume.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/portfolio.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/contact.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/blog.cfm</loc> <priority>0.5</priority> </url> </urlset>
- After this code, enter a closing </cfxml> tag.
-
Your code should look similar to this:
<cfxml variable="xmlSitemap"> <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.google.com/schemas/sitemap/0.90"> <url> <loc>http://www.myWebsite.com/</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/about.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/resume.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/portfolio.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/contact.cfm</loc> <priority>0.5</priority> </url> <url> <loc>http://www.myWebsite.com/blog.cfm</loc> <priority>0.5</priority> </url> </urlset> </cfxml>
- After the closing </cfxml> tag, create an open <cfoutput> tag.
- On the line after the <cfoutput> tag, write #toString(xmlSiteMap)#.
- After the toString line of code, write a closing </cfoutput> tag.
- Open up a browser and navigate to /www/generateSiteMap.cfm.
- View the source of the page and you will see the generated XML.
- Return to your code editor and remove the <cfoutput> tags and their contents.
-
After the closing </cfxml> tag, create a <cffile> tag with the following attributes:
- action: write
- file: #expandPath('./sitemap.xml')#
- output: #toString(xmlSiteMap)#
-
Your code should look similar to this:
<cffile action="write" file="#expandpath('./sitemap.xml')#" output="#toString(xmlSitemap)#" />
- After the <cffile> tag, create a <p> tag. Provide the text "File created!" and place a closing </p> tag after the line of text.
- In your browser, reload the /www/generateSiteMap.cfm page and confirm that you see the message "File Created!"
- Navigate to /www/sitemap.xml and confirm that the XML file was created and that all your XML nodes are displayed.