Editing xml and xml.cfm files in a better Eclipse XML Editor

Friday, February 4, 2011

Lately, the frameworks I’ve been using  have been configured with XML files – ColdSpring, ModelGlue, and ValidateThis. If you’re coding XML, and use either the basic XML editor in Eclipse or the Aptana XML editor in ColdFusion Builder, you’re potentially missing out on some slick code completion offered by another Eclipse plugin… the  WebTools project, which contains my free XML Editor of choice.

To Install:

Use the normal “Help -- Install new software” process in Eclipse, using http://download.eclipse.org/webtools/updates/ as the update site.

What’s it get me?

The WTP XML editor has two must-have features:

  1. It “learns” your XML and begins providing code assist after it knows what elements are in your files
  2. If your XML file contains a schema declaration, it’ll provide very rich code assist for both elements and attributes

Check it:


This is me editing a ModelGlue xml config file. Note that the file has this attribute on the modelglue element: xsi:noNamespaceSchemaLocation="http://www.model-glue.com/schema/gesture/ModelGlue.xsd"

Yes, that’s a valid URL.

This means that if your framework of choice makes an XSD schema available, then you can add the appropriate URI to your XML file and voila, the editor will start providing code assist.

One other small-but-useful feature is automatic element shorthand. Let me explain:

Imagine you type this: <param name=”regex” value=”(?i)password”></param>

Or, rather, imagine the editor auto-closed that tag. Now, if you put your cursor right after password” and before the closing angle bracket, and enter the backslash, the editor will automatically remove the closing element. Small, but the little things in life matter.

How do I edit XML files with a CFM Extension?

A best practice for the XML files is to suffix them with a .cfm extension, i.e. ModelGlue.xml.cfm if those files are web-accessible. This way, you can put an Application.cfm in that directory, with a CFABORT, and those xml config files are no longer readable (assuming you don’t lock them down with your webserver). The problem here is that once they have a .cfm extension, they don’t play nicely with the XML editor.

Fortunately, this is an easy fix.

  1. Open the Eclipse preferences
  2. Filter on “content type”
  3. Scroll down, expand “text”, then go to “XML“
  4. In the bottom pane, click “Add” and then type “.cfm”. Click OK till finished.

Here’s what that looks like:



That’s step 1. Step 2 is actually opening a .cfm file in the WTP XML Editor.

  1. Right click on your .xml.cfm file
  2. Select “Open With”
  3. If '”XML Editor” is an option, choose it. Otherwise, click the “Other” and scroll down till you see the XML editor. It’ll be at the bottom. Note this is not the “Aptana XML Editor”

That dialog looks like this:


This is  a one time thing! Once you open a particular .xml.cfm file in the XML editor, it’ll always open in that editor until you instruct it otherwise.


How does Eclipse know that your .xml.cfm is an XML content type?

You may have a .xml.cfm file that, when you open with the xml editor via right click – open with, continues to give you the “invalid content type” popup. This is most likely because the file does not have the xml declaration at the top. For example, if you have a Coldspring beans.xml.cfm file and it starts with <beans>, you’ll probably get this error. The fix is to add <?xml version="1.0" encoding="UTF-8"?>  to the top of your file.


If you want code assist on your XML files, both .xml and .xml.cfm, then take 5 minutes to install the WTP XML Editor. Give it a go, and see if it’s right for you.


Jamie Krug said...

Good stuff, thanks for sharing, Marc. To any folks new to taking advantage of this type of goodness, I'd point out a couple specific convenience and efficiency benefits, to encourage giving it a try. It can save on typos, since you generally only type a couple characters to start a tag or attribute. Another huge time saver for me is that the code assist is such a fast syntax reminder, and saves a ton of time if you compare it to jumping over to Web documentation or another example file, just for a reminder on a given tag or attribute name.

I'll also mention that I use IntelliJ IDEA a lot, which has great XML support out of the box. I make sure my Web context is set to the proper path for my Web root (not necessarily the same as your project root) and will often use a relative path to an XSD (e.g., xsi:noNamespaceSchemaLocation="/ValidateThis/core/validateThis.xsd"). This gives me code assist without an Internet connection, FWIW.

Marc Esher said...

Thanks for the comments, Jamie. The typo prevention is underrated. XML config files are notorious for causing hard to understand problems precisely because they're "just strings". A better code-assisting editor doesn't prevent all problems -- particularly your typos in the attribute values and element values -- but at least it prevents typos in attribute and element names.

When building a model-glue event, for example, I type perhaps 5 characters to construct all the html, and spend the rest of my time typing the *meaningful* parts... event names, view paths, etc. The WTP editor mitigates a lot of the annoyance of working with XML config.

And darn good point on using a local xsd file for the noNamespaceSchemaLocation. Thanks!

Jamie Krug said...

Hey Marc, my pleasure. When I first got into ValidateThis, the XML config code assist was just a gigantic time saver. Cheers.

Ilya Fedotov said...

Great tips, I was just thinking about this issue, and here is an answer. I couldn't find XSD for coldspring but in CFEclipse one can right click on config file, open cf Frameworks and Set/Unset as configuration files. Handy window will open up, choose ColdSpring and you get that code assist stuff without adding a line in the file. which is not as portable, but helps.