Write in XML with the NetBeans text editor | Opensource.com

Write in XML with the NetBeans text editor

NetBeans is a popular Java IDE, making it a handy text editor for developers already using it.

Coffee beans and a cup of coffee
Image credits : 
Pixabay. CC0.
x

Subscribe now

Get the highlights in your inbox every week.

I’ve spent a considerable amount of time in NetBeans, a Java IDE (integrated development environment) maintained by the Apache Foundation. I find it’s got a good mix of automated abstraction and manual configuration that helps me keep my Java projects organized and optimized. Not all IDEs give much thought to text files, but XML is frequently used in Java projects, so XML support in NetBeans is an important feature. It occurred to me that NetBeans, in addition to being an excellent Java IDE, could make for a nice XML editor, with the added benefit of being contained in a familiar application I already use.

Install

Prior to installing NetBeans, you must ensure you have Java itself installed. The easiest way to install Java is to download a package from adoptOpenJDK.net. This site provides installer packages for all platforms. I prefer to use the LTS (long-term support) release, but if you’re already a Java programmer, then you may have your own preference.

If you’re using Linux and BSD, you can probably install NetBeans from your distributions’ software repository or ports tree. Alternatively, you can install it as a Flatpak from Flathub.

On Windows and Mac (and Linux, if the other methods aren’t to your liking), you can install NetBeans directly from netbeans.org.

XML in NetBeans 

NetBeans supports XML natively, so there are no plugins to install or hidden options to enable. However, NetBeans is project-centric. It assumes, by design, that every file you create belongs to a Java project. This doesn’t have to be true, but every file you create does need to belong to a NetBeans project directory unless you open an existing one from an arbitrary place on your hard drive. You probably won’t be opening NetBeans just to make a quick note to yourself, but if your use case (like mine) is for projects big enough to have an IDE open all day anyway, then NetBeans makes sense.

When you create a new XML Document, you’re prompted to save it into a project directory. After naming your file, it opens as a tab in your NetBeans workspace. NetBeans is schema-aware, so you can set your schema from within an XML document. For example, I love to write in Docbook, so I start my XML files with this declaration:

<?xml version="1.0" encoding="utf-8"?>
<article xmlns="http://docbook.org/ns/docbook"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     dtd="/usr/share/xml/docbook5/schema/dtd/5.0/docbook.dtd"
     version="5.0"
     xml:lang="en">

NetBeans recognizes this as a schema and namespace declaration and downloads the appropriate files.

Retrieving Location: http://docbook.org/ns/docbook
    Retrieved :    https://docbook.org/ns/docbook
    Saved at: /home/tux/.cache/netbeans/12.1/mavencachedirs/1391771919/retriever/docbook.org/ns/docbook.xml

Writing XML in NetBeans is a pleasure. It understands tags and how and when to close them. Tags are exempted from the spell checker. NetBeans also has a good syntax highlighting theme to make it easy for you to differentiate tag elements, attributes, and content. It provides code folding, so I can easily collapse blocks of text I don’t need to see at any given time. You can hide and reveal folded text by clicking on the - or + icons on the left of each block. Alternatively, you can use the keyboard shortcut Ctrl++ or Ctrl+- .

When you right-click on your editing window, you get an XML-specific menu with options to check your XML syntax and to validate your XML against a schema, and even to apply an XSL transform on your work.

There are many more features in NetBeans that aren’t specific to editing XML but are still useful. For instance, you can split your editing window in NetBeans, so you have multiple files open side by side. Line numbers down the left side of the window make it easy for you to navigate, and you can also create bookmarks in your file and then use them to quickly jump between points. There’s auto-indentation, code completion, an outline view, and a great interface, especially to those already familiar with it as a code editor.

Try NetBeans 

NetBeans isn’t the best choice for a general text editor. It’s big, it has lots of options for very specific languages, and not much for anything else. However, if you write in XML on a regular basis, or if you just use NetBeans anyway, then it works great as a handy text editor. Why open a separate application when you have everything you need in your IDE already? Give NetBeans as a text editor a try!

Person drinking a hot drink at the computer

This editor packs the power of Java and offers plenty of plugins to streamline your workflow.
Text editor on a browser, in blue

This basic editor is geared toward web developers, supporting multiple programming languages and offering plenty of extensions to make it your own.
Business woman on laptop sitting in front of window

Vi is the quintessential Unix text editor. Get to know it—or any of its incarnations, Vim, Neovim, gVim, nvi, or Elvis, for Linux, macOS, Windows, or BSD.

About the author

Seth Kenlon
Seth Kenlon - Seth Kenlon is a UNIX geek, free culture advocate, independent multimedia artist, and D&D nerd. He has worked in the film and computing industry, often at the same time. He is one of the maintainers of the Slackware-based multimedia production project Slackermedia.