Hack 5 Examine XML Documents in Text Editors

figs/beginner.gif figs/hack5.gif

Even plain-text editors offer features that make editing XML documents a pleasure. This hack introduces two options, Vim and Emacs with nXML.

XML has been called "Unicode with pointy brackets." As such, XML documents can be displayed in your average, run-of-the-mill, non-graphical text editor. Of course you could view, create, and edit XML documents in Notepad on Windows, but it's not a very exciting editing environment (see http://tucows.com/htmltext95_default.html for examples of other text editors).

There are a number of text editors that are quite suitable for working with XML. We'll talk about two of them here: Vim (Vi improved, a clone of Vi) and Emacs. Both are free for the downloading.

If you are accustomed to a point-and-click, graphical user interface for editing text [Hack #6], you probably won't like using Vim or Emacs with XML. If, however, you prefer typing at the keyboard over clicking the mouse (like me), this hack is for you.

1.6.1 Vim

Vim (http://www.vim.org) is a derivative of the Unix screen editor, Vi. It is currently at Version 6.3 and is developed under the leadership of Bram Moolenaar. You can get flavors of Vim that run on Unix (such as Red Hat, Sun Solaris, or Debian), Windows, MS-DOS, the Mac, OS/2, and even Amiga (downloads available at http://www.vim.org/download.php). If you are running recent versions of Red Hat (http://www.redhat.com) or Cygwin for Windows (http://www.cygwin.com), you likely already have Vim installed on your system.

Vi was developed by Bill Joy et al. in the late 1970s for Unix (http://www.cs.pdx.edu/~kirkenda/joy84.html). Vi was the first screen editor I ever used?back in 1983?and I still use Vim almost every day. Vim is powerful, and without elaborating on all the reasons why I like to use Vim, I will mention just one: syntax highlighting.

Sure, syntax highlighting is available in other editors, but Vim supports over 300 languages with syntax highlighting. Syntax highlighting helps you see clearly that what you are typing is correct because it assigns colors to the correct syntax of a given language, such as XML. This can help you detect typing errors readily. (See a FAQ on Vim syntax highlighting at http://vimdoc.sf.net/cgi-bin/vimfaq2html3.pl#24.1.) Hans Fugal's advanced hack on Vim is available in the next chapter ( [Hack #13] ).

If Vim is already installed and in the path, type the following line:

vim -h

This will return Vim's command-line options. If you need help installing Vim, see http://vimdoc.sourceforge.net/htmldoc/usr_90.html.

To bring up the XML file time.xml in Vim, type the following on a command line while in the working directory:

vim time.xml

Figure 1-8 shows Vim in a terminal window under Gnome on Red Hat.

Figure 1-8. Vim in a terminal window under Gnome on Red Hat

1.6.2 Emacs with nXML

Another text editor that you might consider for editing XML is Emacs. For simple installation on Windows or Unix/Linux, try the Text Coding Initiative's (TEI) version of Emacs (http://www.tei-c.org/Software/tei-emacs/); otherwise, try GNU Emacs for Windows, Unix/Linux, and an even broader range of platforms (http://www.gnu.org/software/emacs/emacs.html). Emacs is a rich and powerful editing environment. It was the second editor I used, also in 1983, under the TOPS 20 operating system on a DEC 20 system. (Yes, I know I'm dating myself.)

Editing XML with Emacs is a pleasure with James Clark's nXML [Hack #12] . It's included in the TEI version, but you can also obtain a copy of nXML from http://www.thaiopensource.com/download/). Figure 1-9 [Hack #72]

Another important major mode for editing XML in Emacs is Lennart Staflin's PSGML (http://www.lysator.liu.se/~lenst/about_psgml/ and http://psgml.sourceforge.net/). This major mode for Emacs provides structural editing, built-in validation with a DTD (for both XML and SGML), menus and commands for inserting tags, error reporting, and more.

Figure 1-9 shows TEI Emacs with nXML editing time.xml on Windows XP.

Figure 1-9. TEI Emacs with nXML editing time.xml

1.6.3 See Also

  • Vim documentation: http://vimdoc.sourceforge.net/

  • The Vim FAQ: http://vimdoc.sourceforge.net/cgi-bin/vimfaq2html3.pl

  • Lennart Staflin's "Editing SGML with Emacs and PSGML": http://www.lns.cornell.edu/public/COMP/info/psgml/psgml_toc.html

  • Bob DuCharme's PSGML tricks: http://www.snee.com/bob/sgmlfree/emcspsgm.html