<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Pieces of Web &#187; xml</title>
	<atom:link href="http://danengle.us/tag/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://danengle.us</link>
	<description>Dan Engle's Rails and Web Development Blog</description>
	<lastBuildDate>Fri, 29 May 2009 21:46:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Generating custom XML for your rails app</title>
		<link>http://danengle.us/2009/05/generating-custom-xml-for-your-rails-app/</link>
		<comments>http://danengle.us/2009/05/generating-custom-xml-for-your-rails-app/#comments</comments>
		<pubDate>Thu, 14 May 2009 00:46:42 +0000</pubDate>
		<dc:creator>Dan</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[builder]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://danengle.us/?p=83</guid>
		<description><![CDATA[Rails provides a very simple way to generate xml.  Most of the time though, it is much too verbose.  Current information obtained by google does not seem to provide all the info needed to generate custom xml.  Keep reading to find out how to generate your own custom xml for your rails 2.3 apps.]]></description>
			<content:encoded><![CDATA[<p>Recently, I needed to generate some xml for a rails 2.3 app.  What was supposed to be a fast and easy, turned out to take much longer than expected.  After all, rails already provides</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#008000; font-style:italic;"># index.html.erb</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@posts</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The problem is that the generated xml is very verbose.  I spent some time googling and found plenty of information, but it was scattered and didn&#8217;t quite show the whole picture.</p>
<p>The rails api for <a href="http://api.rubyonrails.org/classes/Builder/XmlMarkup.html">Builder::XmlMarkup</a> uses both an &#8216;xm&#8217; and &#8216;xml&#8217; object to show how to work with xml, but only the &#8216;xml&#8217; object seems to work.  It also doesn&#8217;t clearly show how to setup the controller or that the code should go in an xml.builder file. Similarly <a href="http://www.tutorialspoint.com/ruby-on-rails/rails-and-xml.htm">here</a>, the xml object is used in the view correctly, but also fails mention anything about the controller or type of file the code should go in.</p>
<p>Over at <a href="http://www.xml.com/pub/a/2007/01/17/making-xml-in-a-rails-app-xml-builder.html?page=2">xml.com</a>, the tutorial creates an @xml instance variable of type Builder::XmlMarkup.new and then uses that object in an .rxml view (I know, its a little dated).</p>
<p>I was left with a bunch of puzzle pieces that didn&#8217;t quite fit together.  Eventually, I figured out what needed to be done, and this is how&#8230;</p>
<p>First, in the controller, remove the other bits in the format.xml line so it reads</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#008000; font-style:italic;"># index.html.erb</span>
  <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span> <span style="color:#008000; font-style:italic;"># index.xml.builder</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Second, you need to create an xml.builder file, in this case, index.xml.builder.  Inside the xml.builder view, you have access to an &#8216;xml&#8217; object that is used to generate the xml.  The xml for my sample @posts object can then look something like&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">xml.<span style="color:#9900CC;">instruct</span>!
xml.<span style="color:#9900CC;">posts</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  <span style="color:#0066ff; font-weight:bold;">@posts</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>post<span style="color:#006600; font-weight:bold;">|</span>
    xml.<span style="color:#9900CC;">post</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      xml.<span style="color:#9900CC;">title</span> post.<span style="color:#9900CC;">title</span>
      xml.<span style="color:#9900CC;">body</span> post.<span style="color:#9900CC;">body</span>
      xml.<span style="color:#9900CC;">published_at</span> post.<span style="color:#9900CC;">published_at</span>
      xml.<span style="color:#9900CC;">comments</span> <span style="color:#9966CC; font-weight:bold;">do</span>
        post.<span style="color:#9900CC;">comments</span>.<span style="color:#5A0A0A; font-weight:bold;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>comment<span style="color:#006600; font-weight:bold;">|</span>
          xml.<span style="color:#9900CC;">comment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
            xml.<span style="color:#9900CC;">body</span> comment.<span style="color:#9900CC;">body</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Now, when browsing to posts.xml, you&#8217;ll now see nicely formatted xml, without all the extra cruft added when using the default rails xml generation utility.  I hope that saves you some time.</p>
]]></content:encoded>
			<wfw:commentRss>http://danengle.us/2009/05/generating-custom-xml-for-your-rails-app/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
