Home & blog  /  Tag: JSONP  /

Presenting XMLTree v.2.0

posted: 09 Feb '12 18:51 tags: XML, tree, traversal, node, JSONP, web service

You may remember XMLTree, which I launched several months ago. In short, it's a plugin that generates a tree from XML data, allowing you to traverse and interrogate the data but also bind events to the various branches of the tree.

Version 2.0 is now here, and comes with a host of powerful new features, including:

Sub-trees: loading data from a web service

Sub-trees allow you to load data branch-by-branch rather than all at once. This means you can use XMLTree with a web service, which serves up the root-level data on load, and then branch-specific data as and when branches are expanded.

Imagine you have a tree showing the boroughs of London. When a branch is expanded, it should show all the bus numbers that serve that borough.

Let's assume the web service returns the following XML format for the root data:

1<boroughs>

2     <borough id='1'>Wandsworth</borough>

3     <borough id='2'>Hackney</borough>

4     <borough id='3'>Croydon</borough>

5     <!-- ... ... -->

6</boroughs>

Knowing that, here's our tree instantiation:

1var ws_url = 'my_web_service.php';

2new XMLTree({

3     fpath: ws_url,

4     container: '#tree',

5     attrsAsData: 'borough_id',

6     subTreeBranches: true,

7     subTreeRequest: function(li) {

8         return ws_url+'?borough_id='+li.data('borough_id');

9     }

10});

By using the attrsAsData param (see below), we ensure that any borough_id attributes from the XML are transfered over as element data onto the tree LIs.

That's critical, because it means that, when a branch is expanded, we can look up that piece of data to determine URL of the web service for loading the next set of data.

The URL is determined and returned by the subTreeRequest param, a callback function that fires when a sub-tree-able branch is expanded and to which the expanded branch's LI is automatically passed. So the URL returned will be something like my_web_service.php?borough_id=13

In summary: to start with, only the root-level data is loaded. The data that goes within branches is determined only when a branch is expanded, by making a separate call to the web service. Crucial information is passed to the web service so it knows what data is returned - in this case, the ID of the clicked branch.

Transfer XML attributes to classes/element data

Attributes in your XML can now be transfered over to the resultant mark-up - in the form of either classes or element data attributes on the branch LIs.

In either case, you can either specify that all attributes should be transferred over, or just some.

Other features

Other, more minor features in this release include:

- support for loading XML over JSONP internet requests

- ability to auto-open the tree at a specific point once it's loaded

- ability to output, ignore or output but hide attributes as branches

Head over here to download, get usage info or view a demo.

post a comment