0.0
No commit activity in last 3 years
No release in over 3 years
This gem adds a `to_hash` method to Nokogiri XML nodes into a Ruby hash. In the resulting hash, all keys are constants. 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.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.10
~> 10.0
~> 2.4

Runtime

>= 1.6, ~> 1
 Project Readme

XML to Hash

Build Status Code Climate Test 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:

gem 'xml-to-hash'

And then execute:

$ bundle

Or install it yourself as:

$ gem install xml-to-hash

Usage

require 'xml/to/hash'
require 'json'

xml_string = STR_XML = <<-EOS
                       <!DOCTYPE dtd-name [
                          <!ENTITY entity_1 "Has been ëxpÄnded">
                          <!ENTITY entity_system SYSTEM "mt_cook1.jpg">
                          <!ENTITY name_public PUBLIC "entity_public_id" "URI">
                          <!NOTATION notation-system SYSTEM "notation-id-system">
                          <!NOTATION NoTaTiOn-PuBLiC PUBLIC "notation-id-public">
                          <!ELEMENT div1 (head,
                       (p | list | note)*,
                       div2*)>
                          <!ELEMENT photo (hello)>
                          <!ATTLIST photo some-attribute CDATA #REQUIRED>
                          <!ATTLIST
                            photo photo_att ENTITY #IMPLIED
                            photo NOTATION (notation-system | NoTaTiOn-PuBLiC | notation-system) #IMPLIED>
                       ]>

                       <myRoot xml:id="root" xml:lang="en">
                             some text
                             <!--
                             In comments we can use ]]>
                             <
                             &,
                       ',
                       and ",
                       but %MyParamEntity; will not be expanded-->
                             <![CDATA[
                             Character Data block <!-- <,
                       & ' " -->  *and* %MyParamEntity;
                             ]]>
                             <?linebreak?>
                             <deeper xmlns="lol://some-namespace" how-deep="very-deep">☠☠☠randomtext☠☠☠
                             <even
                               lol:my-attr="just an attribute"
                               xmlns:lol=\'lol://my.name.space/\' deeper="true">&amp;</even></deeper>
                       </myRoot>
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

{
  "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 <!-- <,\n                       & ' \" -->  *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.

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.

License

The gem is available as open source under the terms of the MIT License.