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

advertisement

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

NetBeans: Working with XML, Part 2

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 two of this three-part series on working with XML, excerpted from NetBeans: The Definitive Guide, go beyond editing XML in your editors, and within the open source NetBeans framework.

Beyond Editing XML

Let's look at the other features in addition to the editors that NetBeans provides for working with XML documents.

Checking and Validating XML

When an XML document is opened in the Explorer, it's automatically checked for syntax. If you change an XML document in the text editor, it's a good idea to run the syntax check manually. Right-click anywhere in the text editor or on the top-level node of an XML document in the Explorer or tree editor and then select Check XML. Any errors will be identified in the Output window.

You can also select Validate XML from the context menu. Validation verifies that the XML is fully and correctly described in a DTD. Do it now with our sample XML document. Right-click and select Validate XML. The output window will show lots of errors because the DTD section of our example is empty. Unfortunately, adding XML data elements does not generate corresponding DTD. More unfortunately, you can't use the tree editor to build your DTD.

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 1
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.

If a suitable external DTD file exists, you can simply assign the external DTD to your XML document. We do not yet have such a file, but let's walk through the process for future reference. In the Explorer window right-click the node for the XML file that needs DTD. That's Inventory in our example. In the context menu select Add Document Type to open the Add Document Type dialog. If the DTD file is accessible remotely, enter its URL in the Public ID: text field. Or, if it's in the local filesystem, enter its name in the System ID: text field. Because we don't have either choice available yet, just click Cancel.

The only other choice is to type in the DTD information manually in the text editor. So you had better know what you're doing, and the Check XML feature will definitely come in handy. Replace the <!DOCTYPE> instruction of your sample XML document with the text in Example 11-2. Don't forget to Check XML and Validate XML.

Example 11-2: DTD text for Inventory.xml

<!DOCTYPE Inventory [
<!ELEMENT Inventory (Supplier|Kit|Part)*>
<!ATTLIST Inventory
    Description CDATA #IMPLIED
  >

<!ELEMENT Kit (Part)*>
<!ATTLIST Kit
    Description CDATA #IMPLIED
  >

<!ELEMENT Part ANY>
<!ATTLIST Part
    Description CDATA #IMPLIED
    Color CDATA #IMPLIED
    Size CDATA #IMPLIED
  >

<!ELEMENT Supplier (Part)*>
<!ATTLIST Supplier
    Description CDATA #IMPLIED
  >
]>

Setting the Node View

Now that you've added a valid DTD to the sample XML document, let's look at its node view. The Explorer window and the Source Editor tree view give exactly the same node view of an XML file, so it doesn't matter which one you work in. Open the top-level node of Inventory and then open the two nodes below, also named Inventory, one for the document's DTD, and one for its data. If you compare the tree view to the text view, you'll find the elements and attributes in the data section match up, one to one in the same order. But the DTD section is different. In the tree view all the elements are listed first under the ElementDeclLayer node, and the attributes are listed separately under AttlistDeclLayer. The difference is determined by the document's Node View setting. To access the Node View setting (as in Figure 11-4), Right-click the top Inventory node, select Properties from the context menu, and click the View tab.

Figure 11-4. DTD and data nodes

Try out the different views. The Linear Node View shows DTD element and attribute declarations in the same order as the text document. The Data Only Node View has the same order but hides comments and processing instructions. You can set the node view at any level, from the topmost node to the lowest, to completely customize the overall view of a large XML document. Setting a node's view to (reset to parent's value) gives the view the same setting as the next higher level. For the top level, (reset to parent's value) applies the default, Grouped DTD Node View.

Generating a DTD

Several XML features require a DTD in a separate file, instead of an internal DTD. So there's a way to split an XML document with an internal DTD into a separate DTD file and a data-only XML document. Right-click the top-level Inventory node, and select Generate DTD from the context menu. A little dialog will pop up prompting you to name the new DTD file. Accept the default Inventory_Inventory, click Yes in the next dialog (Do you want to use generated DTD as external document type?), and the original XML with DTD document is split neatly in two (see Figure 11-5). Now, you can create as many XML data files as desired, all validated by the same single DTD file. And you have the DTD input needed to use the following features.

Figure 11-5. DTD separated from an XML document

Generating Documentation

Having a separate DTD file makes it possible for any number of XML documents to use it and for developers to design any number of applications to use the data that it describes. We need documentation that developers can read to help them build their applications. The IDE can generate an excellent HTML framework for building documentation. Just right-click the DTD file Inventory_Inventory, and select Generate Documentation from the context menu. Again a dialog pops up prompting you to name the new documentation file. Accept the default Inventory_InventoryDocumentation, and the HTML file shown in Example 11-3 is generated to document the DTD. Use the NetBeans editor or any HTML editor to enhance the documentation as needed.

Example 11-3: HTML documentation for Inventory.xml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>

<title>DTD Grammar Documentation</title>
<meta http-equiv="Content-Type" content="text/xhtml; charset=UTF-8" />
</head>

<!-- Generated on May 11, 2002 by NetBeans XML module. -->
<body>

<hr />
<h2>Element Index</h2>
<ul><li><tt><a href="#Inventory">Inventory</a></tt></li>
<li><tt><a href="#Kit">Kit</a></tt></li>
<li><tt><a href="#Part">Part</a></tt></li>
<li><tt><a href="#Supplier">Supplier</a></tt></li>
</ul>

<hr />
<h2>Element Details</h2>

<hr />
<h2><a name="Inventory"></a>Inventory</h2>

<p><b>Declared Attributes</b></p>
<ul><li><tt>#IMPLIED CDATA Description</tt></li>
</ul>
<p><b>Element Content Model</b></p>
<p><tt>(<a href="#Supplier">Supplier</a>&nbsp;|
  <a href="#Kit">Kit</a>&nbsp;| 
  <a href="#Part">Part</a>)*</tt></p>
<p><b>Referenced by</b></p>
<p><tt></tt></p>

<hr />
<h2><a name="Kit"></a>Kit</h2>

<p><b>Declared Attributes</b></p>
<ul><li><tt>#IMPLIED CDATA Description</tt></li>
</ul>
<p><b>Element Content Model</b></p>
<p><tt>(<a href="#Part">Part</a>)*</tt></p>
<p><b>Referenced by</b></p>
<p><tt><a href="#Inventory">Inventory</a></tt></p>
 
<hr />
<h2><a name="Part"></a>Part</h2>

<p><b>Declared Attributes</b></p>
<ul><li><tt>#IMPLIED CDATA Description</tt></li>
<li><tt>#IMPLIED CDATA Color</tt></li>
<li><tt>#IMPLIED CDATA Size</tt></li>
</ul>
<p><b>Element Content Model</b></p>
<p><tt>(#PCDATA)*</tt></p>
<p><b>Referenced by</b></p>
<p><tt><a href="#Inventory">Inventory</a>, 
  <a href="#Kit">Kit</a>,
  <a href="#Supplier">Supplier</a></tt></p>

<hr />
<h2><a name="Supplier"></a>Supplier</h2>

<p><b>Declared Attributes</b></p>
<ul><li><tt>#IMPLIED CDATA Description</tt></li>
</ul>
<p><b>Element Content Model</b></p>
<p><tt>(<a href="#Part">Part</a>)*</tt></p>
<p><b>Referenced by</b></p>
<p><tt><a href="#Inventory">Inventory</a></tt></p>
</body></html>

Generating CSS

Similarly, you can easily generate a CSS document. Right-click the DTD file Inventory_Inventory, select Generate CSS from the context menu, accept the default name Inventory_InventoryStylesheet, and the CSS file shown in Example 11-4 is generated based on the DTD.

Example 11-4: Cascading Stylesheet for Inventory.xml

/* Cascade style sheet based on Inventory_Inventory.dtd DTD */
Inventory { display: block }
Kit { display: block }
Part { display: block }
Supplier { display: block }

Like the HTML file generated above, this CSS file is a framework that a developer can enhance as needed. The essential purpose of an XML document is to store information content in a standard format that is independant of any presentation appearance. Before the information can be viewed, it must be transformed into HTML, WML, or some other format that can be rendered by a browser, printer, or other presentation device. The essential purpose of a CSS document is to control the transformation process, making it possible to view information from an XML document in a comfortable format. The stylesheet information in Example 11-4 simply states that information from each element in Inventory.xml should be presented in a separate block of text. You could enhance the CSS file by adding specifications to give different font properties to each element type. For example, information from Kit elements could be big and green, and information from Part elements could be small and red.

In the next installment, learn how to generate Java classes.


Return to ONJava.com.