Saturday, January 2, 2010

Eclipse as an XML editing tool

A good XML editing tool should ideally be syntax-aware, context-sensitive, graphical and support namespaces to enhance productivity. Any experimentation with a simple text-editor like Notepad brings out all the frustration when the edited document acquires any complexity. It becomes tedious to indent the tags to improve readability, difficult to reorganise structures and a nightmare to debug errors. You may be a genius at handling XML but the fitness for purpose of a simple text-editor is undeniably questionable. Try converting a sub-element in an XML Schema instance into an attribute in a text-editor to understand the radical surgery required to achieve this simple task and the potential for errors. There are good commercial products like Stylus Studio (Progress Software Corporation) available to handle all aspects of XML development effectively but a free, feature-rich, open source product, namely Eclipse, is also admirable in its capabilities. Just look into Help\Software Update\Manage Configuration section of Eclipse to get a feel for the extraordinary variety of features provided by this integrated development environment. I will focus on three features which add value in editing XML documents and XML Schema instances.

Firstly, the syntax awareness is pervasive throughout the product. When we look at a simple xml document in Eclipse, it is immediately apparent that the elements are shown in one colour and their values in another and same is applicable to attributes. This colour scheme for handling all the colour preferences for different syntax elements is governed through the preferences management. Also if the document is linked to a schema, it automatically validates the content against the schema and underlines the text in the manner of wrong spellings in Word if the added content doesn’t adhere to the constraints of the data-type of the element. Incidentally, the contents which are not dictionary words are also underlined but in a different colour to support correction of possible mis-spelt words. The other dimension of this syntax awareness is context sensitivity.For example, when I type <>

The second major feature is the support for graphical editing. There is ‘outline’ view available which allows us to easily navigate through the tree structure by letting us expand and collapse various nodes. Clicking on any node in this outline view takes us to the corresponding element in the XML document. If this were not enough, we can toggle between design and source view in the main editing panel. In the design view, which is a refinement of outline view with content, we can easily edit the contents, whilst the source view lets us rapidly cut and paste appropriate sections. Right click on any node in design or outlook view allows us to add appropriate processing instructions, comments, elements etc.

The IDE automatically synchronises the editing place in these two distinct views to correctly reflect changes. Incidentally, the design view is displayed slightly differently graphically while editing an XML Schema instance. Carrying on the context-relevance theme, it shows sections relevant to defining a schema and their relationships. Right clicking on any of the constituents of these sections brings up a pop-up window for handling the type, ie for an element we may be able to set multiplicity while for a complex data type we may be able to add/remove elements or attributes. Also double clicking on any constituent of a section takes us to see its full definition graphically. All this has the beauty of letting us focus on one aspect type thoroughly. Once we get down to an element we can define constraints in graphic view like maximum length, enumerations etc and see the code appear automatically in the text mode. The only caution I will add is that design and source view can occasionally fall out of sync in an incomprehensible fashion. I particularly recall cutting and pasting a schema in my environment and playing with it. It insisted that a definition of an attribute from original schema were in my schema instance whereas the source view clearly showed that there was no such attribute. I believe this stems from multiple tools working together to harness the full power of XML and they occasionally don’t behave as well as one may expect.

The third impressive feature is the general XML support. We can easily click on format button to auto-indent the elements to improve readability. Or cleanup a document by automatically compressing empty element tags, or inserting required attributes, or inserting missing tags, or adding quotes around attribute values. The validate option can check a document against it schema and check it is well-formed. The refactor option allows a tag to be renamed through out a project to make it more meaningful. The schema definition template automatically refers to the appropriate URI http://www.w3.org/2001/XMLSchema. In fact, the various templates themselves are customisable. An XML document can be created from an existing schema and various options are allowed during creation. Also a catalogue of standard schemas is available. These XML support features can have occasional hiccup. I remember that a reformat of text in text-editor made a valid document against a schema invalid because the reformatting inserted a tag value on newline to make it more readable but it made it invalid against the schema as the content was from an enumeration and it didn’t like these extra whitespaces.

It should be clear that despite the small niggles the synergistic impact of these features makes Eclipse a potent environment for editing XML documents and XML Schema instances. However, it should also be noted that fans of NetBeans will notice that the similar features are available in that tool.

No comments:

Post a Comment