jQuery XML to JSON Pluginv1.0 |
|
The XML to JSON Plugin (jQuery.xml2json) is a script you can use to convert simple XML into a JSON object.
Convert this...
|
...into this:
|
With XML in a stringClick here to test this code
|
With XML loaded via AjaxClick here to test this code
|
Those of you with a little more jQuery experience will know that in simple cases (such as the above) we could use jQuery DOM traversing functionality to achieve the same result without the need of a plugin:
alert($('<xml><message>Hello world</message></xml>').find('message').text());
However, jQuery's DOM traversing can soon become a little tiring
if...
A. you frequenly process XML responses from Ajax calls
B. you parse complex/large XML documents (such as RSS feeds)
...in which case this plugin is perfect for you.
I made this plugin for my own convenience when processing XML files and I hope the benefits will become more aparent as you work your way through the examples on this page.
Consider the following XML file (data/animals.xml):
<?xml version="1.0" encoding="utf-8"?>
<animals>
<dog color='Black'>
<name>Rufus</name>
<breed>labrador</breed>
</dog>
<dog breed='whippet'>
Adopted
<name>Marty</name>
</dog>
<cat color="White">
<name>Matilda</name>
</cat>
</animals>
$.get('data/animals.xml', function(xml){
var animals = $.xml2json(xml);
alert(animals.dog[1].name +'/'+ animals.dog[1]);
});
In simple mode, the plugin will only use arrays and objects when necessary. It also means you don't have to use .text (or .nodeValue) to retrieve the text of each node.
Resulting JSON object
|
Accessing the data
|
$.get('data/animals.xml', function(xml){
var animals = $.xml2json(xml, true);
alert(animals.dog[1].name[0].text +'/'+ animals.dog[1].text);
});
In extended mode, the plugin converts each and every node into an array.
Resulting JSON object
|
Accessing the data
|
This is how the plugin works in simple mode (default) - which represents the XML data in a structure that is as simple and easy to use as possible.
| FILE: data/notes.xml | FILE: data/menu.xml | FILE: data/catalog.xml |
The benefit of this behaviour is that arrays are only used when necessary.
So you can do this...
|
...instead of this:
|
This is how the plugin works in extended mode - where each and every node is an array.
| FILE: data/notes.xml | FILE: data/menu.xml | FILE: data/catalog.xml |
Simple, just add 'true' to the second parameter when you call the plugin, like this:
// ... get your xml data
var json = $.xml2json(xml, true /* extended structure */);
// ... do some stuff
You can load and format an RSS feed just like you would work on any other XML file. The plugin loads the structure onto an easy to use JSON object so you can easily loop through every item in the rss channel like this:
|
This is div#test-rss. Results of the test above will be shown here |
In simple modeFILE: data/rss.xml |
In extended modeFILE: data/rss.xml |
| Package: |
v1.0
xml-to-json.zip
previous versions - |
| Files: |
These are the individual required files (already included in the zip package above)
|
| jQuery: | jquery-latest.js (see jQuery.com) |
<script src="jquery-latest.js" type="text/javascript" language="javascript"></script>
<script src="jquery.xml2json.js" type="text/javascript" language="javascript"></script>
Quick Support Links: Help me! | Report a bug | Suggest new feature
Support for this plugin is available through the jQuery Mailing List.
This is a very active list to which many jQuery developers and users subscribe.
Access to the jQuery Mailing List is also available through the
Nabble Forums, the
jQuery Google Group and the
official project page.
As mentioned in the introduction, you may not need this plugin if
all you're going to be doing is some very simple ad-hoc XML handling.
Whether or not that is the case, I thoroughly recommend this amazing article:
Easy XML handling with jQuery (without a plugin)
Feel free to post suggestions in the jQuery Mailing List. (Yes, I will check it!)
| Attribution link: | (c) Fyneworks.com |
| HTML Code: |
| XML to JSON Plugin by Fyneworks.com is licensed under the MIT License and the GPL License. | ![]() |
Copyright (c) 2008 Fyneworks.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
|
XML to JSON Plugin
by Fyneworks.com
is licensed under the MIT License and the GPL License. |
[back to top]
|