Looking for a simple way

Looking for a simple way to convert XML to JSON Object

xmlObjectifier is here to help. In just one method call your XML document is converted into a JavaScript Object and is ready for simple data browsing using a familiar dot notation. xmlObjectifier is written in client-side JavaScript in an object oriented manner to reduce processing overhead and to provide optimal compatibility across various browsers.

Announcement: New BETA 2 version of xmlObjectifier!!!

Beta version has been re-written to accommodate prototypal inheritance. This concept minimizes memory footprint for all nodes in JSON tree and ensures consistent functionality for all node helper functions. In addition new helper functions have been added. (same updates apply to stand-alone version). This version also attempts to resolve attribute/node name collisions, which happens when you have an attribute with the same name as a child node – to resolve this issue a string “attr_” is pre-pended to an attribute name whenever there is a name collision. Nodes in BETA 2 now also contain _parent reference field and a _nodeName attribute that tells you the string name of the node you’re working with.
Go to downloads page to get the latest and greatest. For more information regarding new methods take a look at documentation page under BETA Methods

Stand-alone version of xmlObjectifier

This version of xmlObjectifier does not require jQuery library to be present. It uses XMLObjectifier namespace, take a look at the usage example below. Same great features of the original code without extra weight. Click here to download.

How did it all got started?

xmlObjectifier was written to simplify a task of working with XML data provided by web services or just static XML files. Complexity of working with DOM tree impacts code flexibility and increases the size of your script. Taking all of those issues into consideration I have written this little converter that recursively scans the DOM tree and produces a JavaScript Object (JSON) that completely replicates both hierarchy and naming of all the nodes inside your XML file.

This library also adds several very useful abilities not provided by DOM, such as sorting of data within nodes by either an attribute or a node value, ability to access data by name, check for position of the data (indexOf) and more.
How does it work?

It works by taking a DOM Document object as an input parameter, scans through it reading all nodes, values and attributes and dynamically assembles a JSON object. The way JSON object is constructed is that all XML nodes are automatically converted to Arrays baring the name of the XML node they represent, and XML attributes are converted to fields of each array element they belong to. The only possible exception to the name being exactly replicated is when node/attribute names contain dash (“-“) in it’s name, in that case all dashes are converted to underscores (“_”) due to JavaScript compatibility issues (objects can not contain dashes in their names);
When to use it?

I don’t wish to get into this big discussion of what is better to use XML or JSON, but I will give you an example where using xmlObjectifier will seem logical and practical.

Let’s assume your site already has a small web-service that produces XML output, and is used in more places then one. Wasting time to develop another service that will produce the same exact data as JSON may seem a bit impractical as well as time consuming. In this scenario you can just re-use the same service to produce JSON on-the-fly and use it as you may see fit. Another reason you may want to use xmlObjectifier is when you want your data source to be more feature full and functional, like being able to quickly traverse and sort the data prior further GUI manipulation or otherwise. I’m sure you can think of more reasons then that, when you face a certain task – I use it to deliver XML data from various feeds to the front-end table grids.

About library

Included with this library you will find xmlObjectifier itself as well as function that parses and converts a text XML into a DOM Document, this feature may enable you to replicate a concept of data islands across several browsers. Both functions are written as an extension to a jQuery library.

This library has been successfully tested on all major browsers such as Internet Explorer, Firefox and Safari, it also has a proven track record by being deployed on several major sport sites (I’m currently employed by mlb.com) including MLB.com, SNY.tv, Yesnetwork.com and more.