Skip to content

Commit

Permalink
update readme with more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pcai committed Sep 29, 2022
1 parent 780d01d commit 5eec90c
Showing 1 changed file with 66 additions and 42 deletions.
108 changes: 66 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ Nori
[![Gem Version](https://badge.fury.io/rb/nori.svg)](http://badge.fury.io/rb/nori)
[![Code Climate](https://codeclimate.com/github/savonrb/nori.svg)](https://codeclimate.com/github/savonrb/nori)

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.

``` ruby
parser = Nori.new
parser.parse("<tag>This is the contents</tag>")
# => { 'tag' => 'This is the contents' }
```
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:
Expand All @@ -22,64 +14,96 @@ 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.
Make sure Nokogiri is in your LOAD_PATH when parsing XML, because Nori tries to load it when it's needed.

# Examples

Typecasting
-----------
```ruby
Nori.new.parse("<tag>This is the content</tag>")
# => {"tag"=>"This is the content"}

Besides regular typecasting, Nori features somewhat "advanced" typecasting:
Nori.new.parse('<foo />')
#=> {"foo"=>nil}

* "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.
Nori.new.parse('<foo bar />')
#=> {}

You can disable this feature via:
Nori.new.parse('<foo bar="baz"/>')
#=> {"foo"=>{"@bar"=>"baz"}}

``` ruby
Nori.new(:advanced_typecasting => false)
Nori.new.parse('<foo bar="baz">Content</foo>')
#=> {"foo"=>"Content"}
```

## Nori::StringWithAttributes

Namespaces
----------
You can access a string node's attributes via `attributes`.

Nori can strip the namespaces from your XML tags. This feature might raise
problems and is therefore disabled by default. Enable it via:
```ruby
result = Nori.new.parse('<foo bar="baz">Content</foo>')
#=> {"foo"=>"Content"}

``` ruby
Nori.new(:strip_namespaces => true)
result["foo"].class
# => Nori::StringWithAttributes

result["foo"].attributes
# => {"bar"=>"baz"}
```

## advanced_typecasting

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

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

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

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

```

XML tags -> Hash keys
---------------------
## strip_namespaces

Nori lets you specify a custom formula to convert XML tags to Hash keys.
Let me give you an example:
Nori can strip the namespaces from your XML tags. This feature is disabled by default.

``` ruby
parser = Nori.new(:convert_tags_to => lambda { |tag| tag.snakecase.to_sym })
Nori.new.parse('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></soap:Envelope>')
# => {"soap:Envelope"=>{"@xmlns:soap"=>"http://schemas.xmlsoap.org/soap/envelope/"}}

xml = '<userResponse><accountStatus>active</accountStatus></userResponse>'
parser.parse(xml) # => { :user_response => { :account_status => "active" }
Nori.new(:strip_namespaces => true).parse('<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></soap:Envelope>')
# => {"Envelope"=>{"@xmlns:soap"=>"http://schemas.xmlsoap.org/soap/envelope/"}}
```

Dashes and underscores
----------------------
## convert_tags_to

Nori will automatically convert dashes in tag names to underscores.
For example:
Nori lets you specify a custom formula to convert XML tags to Hash keys using `convert_tags_to`.

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

parser = Nori.new(:convert_tags_to => lambda { |tag| tag.snakecase.to_sym })
parser.parse('<userResponse><accountStatus>active</accountStatus></userResponse>')
# => {:user_response=>{:account_status=>"active"}}
```

You can control this behavior with the `:convert_dashes_to_underscores` option:
## convert_dashes_to_underscores

By default, Nori will automatically convert dashes in tag names to underscores.

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

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

0 comments on commit 5eec90c

Please sign in to comment.