Method: Hash.from_xml
- Defined in:
- lib/extlib/hash.rb
.from_xml(xml) ⇒ Hash
Converts valid XML into a Ruby Hash structure.
Mixed content is treated as text and any tags in it are left unparsed
Any attributes other than type on a node containing a text node will be discarded
- Typecasting is performed on elements that have a
typeattribute: - integer
-
Returns an 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
- Simple
-
<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>
Becomes:
{ "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 <em>brown</em> Fox" }
- Attributes other than type on a node containing text
-
<story is-good=‘false’>
A Quick <em>brown</em> Fox</story>
Are ignored:
{ "story" => "A Quick <em>brown</em> Fox" }
- Other attributes in addition to
type -
<bicep unit=‘inches’ type=‘integer’>60</bicep>
Evaluates with a typecast to an integer. But unit attribute is ignored:
{ "bicep" => 60 }
84 85 86 |
# File 'lib/extlib/hash.rb', line 84 def from_xml( xml ) ToHashParser.from_xml(xml) end |