Home & blog  /  Tag: node  /

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

2     '1'>Wandsworth

3     '2'>Hackney

4     '3'>Croydon

5    

6

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

Additions to XML tree; jQuery XML parsing

posted: 24 Nov '11 08:45 tags: XML, tree, traversal, node, parsing

You may have seen my XMLTree plugin, posted a few weeks ago.

If you'll excuse the trumpet-blowing I do find this plugin among my most useful; I'm working on a number of projects right now that involve XML in the browser, and the ability to visualise it in a traversable tree is quiet satisfying.

New features (all documented on the script page) include:

- callback on resultant HTML - you can now specify a callback function which will be invoked after the tree has been generated. It is automatically passed a DOM reference to the tree ul so you can act on it.

- attributes - can now be ignored, hidden or shown. Hiding them is useful if you plan to traverse or interrogate the tree based on their existence or value, without wanting to show them to the user.

Bug fixes

This was an interesting one. With XMLTree you can either point it at an XML file for it to load, or manually pass it a well-formed string of XML.

In the latter case, it exploits a well-known (yet somewhat abused) perk of jQuery - that you can pass it a DOM or XML string and it will turn it into a nodeset.

But because this is intended for DOM creation, not XML creation, it assumes you're parsing an HTML document, not XML. The problem? If your XML contains any tags that share their names with any self-closing HTML tags, jQuery renders your XML invalid.

Consider the following example:

1var xml = "http://mitya.co.uklink>";

2console.log($(xml).html());

The output of that will be:

/>http://mitya.co.uk

...which is obviously not what you'd wanted.

As a fix for this XMLTree now renames (secretly) all of the node names - but restores their original names on output.

Ideally, of course, I wouldn't be lazy and rely on jQuery to parse XML - there are native ways (albeit different ones for different browsers). But for now this fix works just fine.

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

post a comment

XML Tree - visualise and traverse your XML

posted: 18 Oct '11 21:49 tags: XML, tree, traversal, node

XMLTree, just posted in the scripts section, is a utility for visualising and traversing XML in tree format.

You point it to an XML file, or manually feed it XML, and it does the rest, turning your nodes into a traversable tree.

But it's more than that; you can stipulate callbacks to fire when a node is expanded/collapsed and/or clicked. Callbacks are passed certain standard arguments, such as a reference to the node that was clicked and the XPath to it.

Behold! I even made a lovely demo for you involving Nintendo characters:

You can also deep-link to a point or points within the tree, so that, on load, the tree opens at a specific point or points. By extension, this means the tree is refresh-friendly, i.e. it won't forget which nodes were open when you reload the page.

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

1 comments | post new