Skip to content


Repository files navigation


CI Gem Version Code Climate

Really simple XML parsing ripped from Crack, which ripped it from Merb.

Nori supports pluggable parsers and ships with both REXML and Nokogiri implementations.
It defaults to Nokogiri since v2.0.0, but you can change it to use REXML via: => :rexml)  # or :nokogiri

Make sure Nokogiri is in your LOAD_PATH when parsing XML, because Nori tries to load it when it's needed.

Examples"<tag>This is the content</tag>")
# => {"tag"=>"This is the content"}'<foo />')
#=> {"foo"=>nil}'<foo bar />')
#=> {}'<foo bar="baz"/>')
#=> {"foo"=>{"@bar"=>"baz"}}'<foo bar="baz">Content</foo>')
#=> {"foo"=>"Content"}


You can access a string node's attributes via attributes.

result ='<foo bar="baz">Content</foo>')
#=> {"foo"=>"Content"}

# => Nori::StringWithAttributes

# => {"bar"=>"baz"}


Nori can automatically convert string values to TrueClass, FalseClass, Time, Date, and DateTime:

# "true" and "false" String values are converted to `TrueClass` and `FalseClass`."<value>true</value>")
# => {"value"=>true}

# String values matching xs:time, xs:date and xs:dateTime are converted to `Time`, `Date` and `DateTime` objects."<value>09:33:55.7Z</value>")
# => {"value"=>2022-09-29 09:33:55.7 UTC

# disable with advanced_typecasting: false false).parse("<value>true</value>")
# => {"value"=>"true"}


Nori can strip the namespaces from your XML tags. This feature is disabled by default.'<soap:Envelope xmlns:soap=""></soap:Envelope>')
# => {"soap:Envelope"=>{"@xmlns:soap"=>""}} => true).parse('<soap:Envelope xmlns:soap=""></soap:Envelope>')
# => {"Envelope"=>{"@xmlns:soap"=>""}}


Nori lets you specify a custom formula to convert XML tags to Hash keys using convert_tags_to.'<userResponse><accountStatus>active</accountStatus></userResponse>')
# => {"userResponse"=>{"accountStatus"=>"active"}}

parser = => lambda { |tag| Nori::StringUtils.snakecase(tag).to_sym })
# => {:user_response=>{:account_status=>"active"}}


By default, Nori will automatically convert dashes in tag names to underscores.'<any-tag>foo bar</any-tag>')
# => {"any_tag"=>"foo bar"}

# disable with convert_dashes_to_underscores
parser = => false)
parser.parse('<any-tag>foo bar</any-tag>')
# => {"any-tag"=>"foo bar"}