Giter Club home page Giter Club logo

ruby-xml-to-json's Introduction

XML to JSON

Build Status Code Climate Test Coverage

This Ruby gem adds a to_hash and to_json method to Nokogiri XML nodes, allowing us to serialize arbitrary XML nodes to JSON.

This gem also picks up attributes, comments, processing instructions and doctype declarations. The result is wordy, but complete.

As an added bonus, line numbers are included where possible.

Installation

Add this line to your application's Gemfile:

gem 'xml-to-json'

And then execute:

$ bundle

Or install it yourself as:

$ gem install xml-to-json

Usage

require 'xml/to/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
puts JSON.pretty_generate(xml.root) # Use xml for information about the document, like DTD and stuff

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 version.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-json. 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.

ruby-xml-to-json's People

Contributors

digitalheir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

toboter

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.