Looking for a simple way to convert XML to JSON Object
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?
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?
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.
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.