static public Method

Hash.from_xml( xml )

Converts valid XML into a Ruby Hash structure.

xml:A string representation of valid XML

Typecasting

Typecasting is performed on elements that have a "type" attribute of integer::

boolean:anything other than "true" evaluates to false
datetime:Returns a Time object. See Time documentation for valid Time strings
date:Returns a Date object. See Date documentation for valid Date strings

Keys are automatically converted to snake_case

Caveats

  • Mixed content tags are assumed to be text and any xml tags are kept as a String
  • Any attributes other than type on a node containing a text node will be discarded

Examples

Standard

<user gender="m">

<age type="integer">35</age>
<name>Home Simpson</name>
<dob type="date">1988-01-01</dob>
<joined-at type="datetime">2000-04-28 23:01</joined-at>
<is-cool type="boolean">true</is-cool>

</user>

evaluates to

{ "user" =>

    { "gender"    => "m",
      "age"       => 35,
      "name"      => "Home Simpson",
      "dob"       => DateObject( 1998-01-01 ),
      "joined_at" => TimeObject( 2000-04-28 23:01),
      "is_cool"   => true
    }
}

Mixed Content

<story>

A Quick <em>brown</em> Fox

</story>

evaluates to { "story" => "A Quick brown Fox" }

Attributes other than type on a node containing text

<story is-good="fasle">

A Quick <em>brown</em> Fox

</story>

evaluates to { "story" => "A Quick brown Fox" }

<bicep type="integer" unit="inches">60</bicep>

evaluates with a typecast to an integer. But ignores the unit attribute { "bicep" => 60 }

Source Code

# File merb/core_ext/hash.rb, line 64
def from_xml( xml )
  ToHashParser.from_xml(xml)
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.