Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
Created November 20, 2012 21:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save learncfinaweek/4121146 to your computer and use it in GitHub Desktop.
Save learncfinaweek/4121146 to your computer and use it in GitHub Desktop.
Data Handling - Hands On 11

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

  1. Create a new file in the /www/ folder called generateSitemap.cfm.
  2. On line 1, create an open <cfxml> tag and provide the following attribute:
    • variable: xmlSiteMap
  3. 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>	
    
  4. After this code, enter a closing </cfxml> tag.
  5. 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>	
    
  6. After the closing </cfxml> tag, create an open <cfoutput> tag.
  7. On the line after the <cfoutput> tag, write #toString(xmlSiteMap)#.
  8. After the toString line of code, write a closing </cfoutput> tag.
  9. Open up a browser and navigate to /www/generateSiteMap.cfm.
  10. View the source of the page and you will see the generated XML.
  11. Return to your code editor and remove the <cfoutput> tags and their contents.
  12. After the closing </cfxml> tag, create a <cffile> tag with the following attributes:
    • action: write
    • file: #expandPath('./sitemap.xml')#
    • output: #toString(xmlSiteMap)#
  13. Your code should look similar to this:
    <cffile action="write" file="#expandpath('./sitemap.xml')#" output="#toString(xmlSitemap)#" />	
    
  14. After the <cffile> tag, create a <p> tag. Provide the text "File created!" and place a closing </p> tag after the line of text.
  15. In your browser, reload the /www/generateSiteMap.cfm page and confirm that you see the message "File Created!"
  16. Navigate to /www/sitemap.xml and confirm that the XML file was created and that all your XML nodes are displayed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment