



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.
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).
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
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.
//Just include this library
<script language="javascript" src="saXMLUtils.js"></script>
//Using xml objectifier
<script language="javascript">
var root = XMLObjectifier.xmlToJSON(xml_dom_object); //Converts xml dom object to JSON
var xml_doc = XMLObjectifier.textToXML(xml_string); //Converts xml string to xml dom
</script>
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.
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);
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.
//Just include this library
<script language="javascript" src="jquery.js"></script>
<script language="javascript" src="jqXMLUtils.js"></script>
XML: test.xml
<root>
<node test="hello">Value</node>
</root>
JavaScript:
//dom_of_test_xml is DOM object of a test.xml file returned by an AJAX request
var root = $.xmlToJSON(dom_of_test_xml);
var node = root.node[0]; // setting a reference to the first node
alert(node.Text); //alerts 'Value' - Text property contains a value of text node or CDATA within a XML node
alert(node.test); //alerts 'hello' - this is how we retrieve attribute values
For all support question and suggestions please visit jQuery project page or Comments section.
Thank You,
Sam Tsvilik - Author


