title: How do I convert XML into a hash in Rails?
body: <p>How do I convert an XML body to a hash in Ruby?</p>
  
  <p>I have an XML body which I'd like to parse into a hash</p>
  
  <pre><code>&lt;soap:Body&gt;
      &lt;TimesInMyDAY&gt;
          &lt;TIME_DATA&gt;
              &lt;StartTime&gt;2010-11-10T09:00:00&lt;/StartTime&gt;
              &lt;EndTime&gt;2010-11-10T09:20:00&lt;/EndTime&gt;
          &lt;/TIME_DATA&gt;
          &lt;TIME_DATA&gt;
              &lt;StartTime&gt;2010-11-10T09:20:00&lt;/StartTime&gt;
              &lt;EndTime&gt;2010-11-10T09:40:00&lt;/EndTime&gt;
          &lt;/TIME_DATA&gt;
          &lt;TIME_DATA&gt;
              &lt;StartTime&gt;2010-11-10T09:40:00&lt;/StartTime&gt;
              &lt;EndTime&gt;2010-11-10T10:00:00&lt;/EndTime&gt;
          &lt;/TIME_DATA&gt;
          &lt;TIME_DATA&gt;
              &lt;StartTime&gt;2010-11-10T10:00:00&lt;/StartTime&gt;
              &lt;EndTime&gt;2010-11-10T10:20:00&lt;/EndTime&gt;
          &lt;/TIME_DATA&gt;
          &lt;TIME_DATA&gt;
              &lt;StartTime&gt;2010-11-10T10:40:00&lt;/StartTime&gt;
              &lt;EndTime&gt;2010-11-10T11:00:00&lt;/EndTime&gt;
          &lt;/TIME_DATA&gt;
      &lt;/TimesInMyDAY&gt;
  &lt;/soap:Body&gt;
  </code></pre>
  
  <p>I'd like to convert it into a hash like this:</p>
  
  <pre><code>{ :times_in_my_day =&gt; { 
      :time_data = &gt; [
          {:start_time=&gt;"2010-11-10T09:00:00", :end_time =&gt; "2010-11-10T09:20:00" },
          {:start_time=&gt;"2010-11-10T09:20:00", :end_time =&gt; "2010-11-10T09:40:00" },
          {:start_time=&gt;"2010-11-10T09:40:00", :end_time =&gt; "2010-11-10T10:00:00" },
          {:start_time=&gt;"2010-11-10T10:00:00", :end_time =&gt; "2010-11-10T10:20:00" },
          {:start_time=&gt;"2010-11-10T10:40:00", :end_time =&gt; "2010-11-10T11:00:00" }
          ]
      } 
  }
  </code></pre>
  
  <p>Ideally, the tags would convert to snake_case symbols and become keys within the hash. </p>
  
  <p>Also, the datetimes are missing their time zone offsets. They are in the local time zone (not UTC). So I'd like to parse it to show the local offset and then convert the xml datetime strings into Rails DateTime objects. The resulting array would be something like:</p>
  
  <pre><code>{ :times_in_my_day =&gt; { 
      :time_data = &gt; [
          {:start_time=&gt;Wed Nov 10 09:00:00 -0800 2010, :end_time =&gt; Wed Nov 10 9:20:00 -0800 2010 },
          {:start_time=&gt;Wed Nov 10 09:20:00 -0800 2010, :end_time =&gt; Wed Nov 10 9:40:00 -0800 2010 },
          {:start_time=&gt;Wed Nov 10 09:40:00 -0800 2010, :end_time =&gt; Wed Nov 10 10:00:00 -0800 2010 },
          {:start_time=&gt;Wed Nov 10 10:00:00 -0800 2010, :end_time =&gt; Wed Nov 10 10:20:00 -0800 2010 },
          {:start_time=&gt;Wed Nov 10 10:40:00 -0800 2010, :end_time =&gt; Wed Nov 10 11:00:00 -0800 2010 }
          ]
      } 
  }
  </code></pre>
  
  <p>I was able to convert a single datetime with the <code>parse</code> and <code>in_time_zone</code> methods this way:</p>
  
  <pre><code>Time.parse(xml_datetime).in_time_zone(current_user.time_zone)
  </code></pre>
  
  <p>But I'm not quite sure the best way to parse the times while converting the XML into a hash.</p>
  
  <p>I'd appreciate any advice. Thanks!</p>
  
  <h2>Edit</h2>
  
  <p>The code for converting the datetime string into a Rails DateTime object is wrong. That will parse the xml datetime string to the system's timezone offset and then convert that time to the user's timezone. The correct code is:</p>
  
  <p><code>Time.zone.parse(xml_datetime)</code></p>
  
  <p>If the user has a different time zone other than the system, this will add the user's time zone offset to the original datetime string. There's a Railscast on how to enable user timezone preferences here: <a href="http://railscasts.com/episodes/106-time-zones-in-rails-2-1" rel="noreferrer">http://railscasts.com/episodes/106-time-zones-in-rails-2-1</a>.</p>