Today we are happy to announce our contribution to the open source world: the XML2JSON gem!
It all started out as a simple side project (more on that on a future post). One of the problems we faced was the XML format. Being rubyists, we don’t find ourselves at ease around XML, so we decided to convert it to JSON and then work with it.
Unfortunately we didn’t find an easy way to do it. No .to_json magic method :(.
Also, we didn’t find a gem or a library to deal with it. There are libraries out there that solve this problem. One of them is activesupport, that extends the Hash class with a from_xml method. The problem with this gem is that it does not handle the conversion the way we need. We wanted an almost transparent conversion from the XML format to the JSON one.
So we decided to write our own library.
These are the main points that make our library different.

Multiple elements are pluralized

    <?xml version="1.0">
    <root>
        <author>
            <name>Giuseppe Modarelli</name>
        </author>
        <author>
            <name>Andrea D'Ippolito</name>
        </author>
    </root>

becomes

    {
        "root": {
            "authors": [
                {
                    "name": "Giuseppe Modarelli"
                },
                {
                    "name": "Andrea D'Ippolito"
                }
            ]
        }
    }

Nodes attributes do not becomes keys

    <?xml version="1.0">
    <root>
        <link href="http://gmodarelli.com" rel="self" />
    </root>

becomes

    {
        "root": {
            "link": {
                "_attributes": {
                    "href": "http://gmodarelli.com",
                    "rel": "self"
                }
            }
        }
    }

Namespaces are treated like attributes

    <?xml version="1.0">
    <root xmlns:"http://some.namespace.definition">
        <link href="http://www.andreadippolito.it" rel="self" />
    </root>

becomes

    {
        "root": {
            "_namespaces": {
                "xmlns": "http://some.namespace.definition"
            },
            "link": {
                "_attributes": {
                    "href": "http://www.andreadippolito.it",
                    "rel": "self"
                }
            }
        }
    }

When there are both attributes and text inside a node

    <?xml version="1.0">
    <root>
        <link href="http://www.wearemonk.com" rel="self">wearemonk.com</link>
    </root>

becomes

    {
        "root": {
            "link": {
                "_attributes": {
                    "href": "http://www.andreadippolito.it",
                    "rel": "self"
                },
                "_text":  "wearemonk.com"
            }
        }
    }

Naming

We chose _attributes, _namespaces and _text out of our liking, but we provided a way to change them.

    XML2JSON.config do |c|
        c.attributes_key = "my_attributes"
        c.namespaces_key = "my_namespaces"
        c.text_key = "my_text"
    end

The Gem

When we finally got ourself a working version, we thought about every time we solved a big problem with a simple gem install. So we decided to share our little library with the world and turn it into a gem.
The gem is still in its early days of development. As of today it is version 0.4.0 and it can only be installed through github

gem 'xml2json', git: 'git@github.com:monksoftware/xml2json.git'

We are planning to release it to Rubygems (as soon as we find out how :D).
If you have any suggestions, feature requests or bug reports please go to Github and fork the repo!
Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>