Git Product home page Git Product logo

nori's Introduction

Nori

Build Status Gem Version Code Climate Coverage Status

Really simple XML parsing ripped from Crack which ripped it from Merb.
Nori was created to bypass the stale development of Crack, improve its XML parser
and fix certain issues.

parser = Nori.new
parser.parse("<tag>This is the contents</tag>")
# => { 'tag' => 'This is the contents' }

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:

Nori.new(:parser => :rexml)  # or :nokogiri

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

Typecasting

Besides regular typecasting, Nori features somewhat "advanced" typecasting:

  • "true" and "false" String values are converted to TrueClass and FalseClass.
  • String values matching xs:time, xs:date and xs:dateTime are converted to Time, Date and DateTime objects.

You can disable this feature via:

Nori.new(:advanced_typecasting => false)

Namespaces

Nori can strip the namespaces from your XML tags. This feature might raise
problems and is therefore disabled by default. Enable it via:

Nori.new(:strip_namespaces => true)

XML tags -> Hash keys

Nori lets you specify a custom formula to convert XML tags to Hash keys.
Let me give you an example:

parser = Nori.new(:convert_tags_to => lambda { |tag| tag.snakecase.to_sym })

xml = '<userResponse><accountStatus>active</accountStatus></userResponse>'
parser.parse(xml)  # => { :user_response => { :account_status => "active" }

Dashes and underscores

Nori will automatically convert dashes in tag names to underscores. For example:

parser = Nori.new
parser.parse('<any-tag>foo bar</any-tag>')  # => { "any_tag" => "foo bar" }

You can control this behavior with the :convert_dashes_to_underscores option:

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

nori's People

Contributors

barberj avatar cloocher avatar deadprogram avatar der-flo avatar edmz avatar gaaady avatar janahrens avatar jim avatar jnunemaker avatar kajetanowicz avatar kornysietsma avatar moom avatar pengwynn avatar purp avatar robuye avatar rubiii avatar sandro avatar tamalw avatar technicalpickles avatar thomasjachmann avatar timriley avatar tjarratt avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.