https://github.com/digitalheir/ruby-xml-to-hash
Ruby gem to transparently convert XML documents to Ruby hashes (and JSON... and beyond)
https://github.com/digitalheir/ruby-xml-to-hash
hash json ruby xml
Last synced: about 1 year ago
JSON representation
Ruby gem to transparently convert XML documents to Ruby hashes (and JSON... and beyond)
- Host: GitHub
- URL: https://github.com/digitalheir/ruby-xml-to-hash
- Owner: digitalheir
- License: mit
- Created: 2015-06-12T07:54:50.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-06-15T09:47:52.000Z (about 11 years ago)
- Last Synced: 2025-04-14T20:11:50.426Z (about 1 year ago)
- Topics: hash, json, ruby, xml
- Language: Ruby
- Homepage:
- Size: 164 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT
Awesome Lists containing this project
README
# XML to Hash
[](https://travis-ci.org/digitalheir/ruby-xml-to-hash)
[](https://codeclimate.com/github/digitalheir/ruby-xml-to-hash)
[](https://codeclimate.com/github/digitalheir/ruby-xml-to-hash/coverage)
This Ruby gem adds a `to_hash` method to Nokogiri XML nodes, allowing us to convert arbitrary XML nodes to a Ruby hash,
and so also to serialize them to JSON.
This gem also picks up attributes, processing instructions and doctype declarations. The resulting hash is wordy, but complete.
As an added bonus, we include line numbers where possible.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'xml-to-hash'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install xml-to-hash
## Usage
```ruby
require 'xml/to/hash'
require 'json'
xml_string = STR_XML = <<-EOS
]>
some text
*and* %MyParamEntity;
]]>
☠☠☠randomtext☠☠☠
&
EOS
xml = Nokogiri::XML STR_XML
hash = xml.root.to_hash # Use xml.to_hash for information about the document, like DTD and stuff
puts JSON.pretty_generate(hash)
```
produces
```json
{
"type": "element",
"name": "myRoot",
"attributes": [
{
"type": "attribute",
"name": "id",
"content": "root",
"line": 17,
"namespace": {
"href": "http://www.w3.org/XML/1998/namespace",
"prefix": "xml"
}
},
{
"type": "attribute",
"name": "lang",
"content": "en",
"line": 17,
"namespace": {
"href": "http://www.w3.org/XML/1998/namespace",
"prefix": "xml"
}
}
],
"line": 17,
"children": [
{
"type": "text",
"content": "\n some text\n ",
"line": 19
},
{
"type": "comment",
"content": "\n In comments we can use ]]>\n <\n &,\n ',\n and \",\n but %MyParamEntity; will not be expanded",
"line": 25
},
{
"type": "text",
"content": "\n ",
"line": 26
},
{
"type": "cdata",
"name": "#cdata-section",
"content": "\n Character Data block *and* %MyParamEntity;\n ",
"line": 26
},
{
"type": "text",
"content": "\n ",
"line": 30
},
{
"type": "pi",
"name": "linebreak",
"line": 30
},
{
"type": "text",
"content": "\n ",
"line": 31
},
{
"type": "element",
"name": "deeper",
"attributes": [
{
"type": "attribute",
"name": "how-deep",
"content": "very-deep",
"line": 31
}
],
"line": 31,
"namespace": {
"href": "lol://some-namespace"
},
"children": [
{
"type": "text",
"content": "☠☠☠randomtext☠☠☠\n ",
"line": 32
},
{
"type": "element",
"name": "even",
"attributes": [
{
"type": "attribute",
"name": "my-attr",
"content": "just an attribute",
"line": 34,
"namespace": {
"href": "lol://my.name.space/",
"prefix": "lol"
}
},
{
"type": "attribute",
"name": "deeper",
"content": "true",
"line": 34
}
],
"line": 34,
"namespace": {
"href": "lol://some-namespace"
},
"children": [
{
"type": "text",
"content": "&",
"line": 34
}
]
}
]
},
{
"type": "text",
"content": "\n ",
"line": 35
}
]
}
```
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `xml/to/hash.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/digitalheir/ruby-xml-to-hash. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](CODE_OF_CONDUCT).
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).