ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button O'Reilly Book Excerpts: NetBeans: The Definitive Guide

NetBeans: Working with XML, Part 1

Related Reading

NetBeans: The Definitive Guide
By Tim Boudreau, Jesse Glick, Simeon Greene, Vaughn Spurlin, Jack J. Woehr

by Tim Boudreau, Jack J. Woehr, Vaughn Spurlin, Jesse Glick, Simeon Greene

In part one in this series of book excerpts from NetBeans: The Definitive Guide, learn how to work with XML within the NetBeans framework by installing XML support and working with XML editors.

Installing XML Support

NetBeans provides extensive support for developing with XML.[1] But it isn't bundled with NetBeans 3.3.2, so you need to get it from the Update Center. Go to Tools menu Update Center to launch the Update Center Wizard. Check the NetBeans Update Center. You will find several modules under the XML node, CSS Support through XML Tree Editor (see Figure 1). Install them all. After the IDE restarts, look at Help menu Help Sets XML Support for complete instructions on using all the features. In this chapter we'll mention the high points and then build some examples.

Figure 1: Installing modules for XML support


We'll spend the bulk of this chapter delving into the XML support available in NetBeans. Before getting into detail, though, you should get an idea of what is available and how it all fits together. You'll then be ready to dive into specific topics.


In This Series

NetBeans: Working with XML, Part 3
In this final installment on working with XML, excerpted from NetBeans: The Definitive Guide, learn how to generate Java classes.

NetBeans: Working with XML, Part 2
In part two of this three-part series excerpted from NetBeans: The Definitive Guide, go beyond editing XML in your editors, within the open source NetBeans framework.

NetBeans provides a variety of templates to get you started on creating the following XML documents:

  • Plain XML
  • XML with DTD included or referenced
  • DTD alone
  • Cascading Style Sheet (CSS)
  • Extensible Style Sheet (XSL)
  • OASIS Specification XML entity catalog

You can also mount existing XML catalogs that meet other specifications, but only the OASIS specification is explicitly supported for creating new catalogs.

Browsing and Editing

Once you've used a template to create a new XML-based document or mounted an existing document, you will want to browse and edit it. If the XML is well formed, you can open the document's nodes and browse it in the Explorer. NetBeans also provides both a text editor and a tree editor for modifying XML files or for closely examinining their contents. The tree editor gives a node view just like the Explorer view, and specialized property editors that change to match the object type of the currently selected node. Context menus help you keep XML source well formed and consistent with DTD during editing through the Check XML and Validate XML actions, both of which will be explained in later sections.

Generating Documentation

After working hard to create a DTD, you probably want to document it so others can share its value. It's as easy as generating a Javadoc for Java source. Right-click, select Generate Documentation, and it's done. You get an XHTML document that describes the content of the DTD, which you may augment with text describing the business meaning of the XML elements and attributes.

Accessing with Java

Of course, you'll want to access your XML documents with Java to take full advantage of XML's power. The first step is as easy as generating documentation. Right-click the DTD and select Generate DOM Tree Scanner or SAX Document Handler Wizard to generate source for Java classes to access the XML. Use a DOM Tree Scanner to modify XML documents, and use a lighter-weight SAX Document Handler if you only need to read the documents. We'll take a closer look at this throughout the rest of this chapter.

XML Schema Support

XML schema support is coming soon. NetBeans 3.4 will have several XML-related enhancements, including XML document validation by XML Schema. See XML - planned features for NetBeans 3.4 for specifics. NetBeans 4.0 should get you even more excited. See XML - planned features index page for a look further ahead.

Of course, it's impossible for any set of development tools to keep completely up to date with advancing technologies. But NetBeans with its highly modular architecture does a remarkable job of bringing out new features quickly. If you want to speed up the process, learn how to build NetBeans modules from this book and start participating in the open source community.

XML Editors

Let's create an XML document with a DTD. We need a work area, so first create a new Java package named XMLWork. Right-click the package in the Explorer, select New XML and DTD XML with DTD to launch the New Wizard, and create a new XML document from a template. Name it Inventory and then click Finish. The document appears as a node in the Explorer and in a tree editor view of the Source Editor. Open the nodes in both the Explorer and the Source Editor, and you will see that both views are identical. When the top-level node is open, the name Inventory appears three times, first as the name of the entire document, next as DOCTYPE root element name in the DTD, and last as a top-level element name in the XML data (see Figure 2). Right-click the top-level node and select Edit to access the same information as plain text.

Figure 2: The XML tree editor and text editor

Any change made in the tree view is immediately applied to the text view. You can see this by clicking back and forth between the tree view and text view tabs at the bottom of the Source Editor. Let's undock the text view to make it easier to watch. Right-click the text view tab, select Dock View Into New Single Frame, and you can see both views at the same time.

We need to add a few elements and attributes to make our document interesting. Our Inventory document will describe Kits, which contain Parts; Suppliers, which produce the Parts; and Parts, which have Size and Color. Everything will have a Description.

Right-click (in either the Explorer or the Source Editor tree view) the Inventory element node, which is the last node named Inventory. Select from the context menu Add Element. Name it Kit, click OK, and notice that a new element is added inside the top-level Inventory element. Did you see all the choices when the context menu for Add was open? You could add anything from Attribute to Text, everything you need for complete XML data editing. So let's add an attribute. Right-click Inventory again; this time select Add Attribute. Name it Description, and give it the value Kit Inventory. It's often easier to add new attributes while creating an element. Just click the Add button while the Element wizard is open, instead of going directly to the OK button, and the little Add Attribute wizard will open. You can also get to the Add Attribute wizard from the specialized property editor that appears in the right panel of the tree editor whenever an element is selected in the left panel. To add some text to the Part element, right-click, select Add Text and then fill in Part A in Kit.

Continue using the tree view to add elements and attributes (as shown in Figure 3) until the text view of your XML document matches Example 1 (except for insignificant blank lines). You can copy and paste in the tree editor. We want all the Part elements to have the same Description, Part A. Once you've created the first Part A Description, you can just copy and paste into the other Part elements. Of course, you could simply paste Example 1 directly into the text editor. But the purpose of this exercise is to gain experience with the tree editor. With large, complex XML documents the tree editor is much easier to use because it automatically keeps the syntax correct. Moving nodes to restructure the data with copy, cut, and paste operations is quick and accurate.

Figure 3: Adding a new attribute

Example 1: Inventory.xml after adding elements and attributes

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created by vaughn on April 26, 2002, 6:07 PM -->
<!DOCTYPE Inventory [
    <!ELEMENT Inventory ANY>
<Inventory Description="Kit Inventory">
    <Kit Description="The first kit">
        <Part Description="Part A">Part A in Kit</Part>

    <Supplier Description="Our favorite supplier">
        <Part Description="Part A">Part A in Supplier</Part>
    <Part Description="Part A" Size="Just right" 
	         Color="Purple">Part A alone</Part>

Our example is small, but it's enough to demonstrate what NetBeans can do with XML documents. We'll keep it small while creating DTD, CSS, and other files based on its content.

In the next installment, go beyond editing XML.

[1] To learn more about XML processing with Java, or to get an overview of XML-related technologies, see Java & XML, 2nd Edition (O'Reilly), by Brett McLaughlin.

Return to ONJava.com.