More action with Struts 2
In a recent review of Struts 2 in Action, JW Blogger Oleg Mikheev notes that Struts 2 is "just a collection of extensions built upon WebWork, which is ultimately the right thing to learn before starting a Struts 2 project." While Struts 2 has some architectural flaws, Oleg calls WebWork well-designed, well-tested, and reliable. What are your experiences using Struts 2 and WebWork?

Also see "Hello World the WebWork way," a JavaWorld excerpt from WebWork in Action, by Patrick Lightbody and Jason Carreira.

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

XML and Java: A potent partnership, Part 2

Learn how to use Java to build applications that handle XML's extensibility

Last month I presented my case for the place of XML in the enterprise (for last month's column, see Resources). I intentionally tried to look beyond the publishing aspects of XML in order to focus on application integration and data exchange issues. I demonstrated how easy it is to parse and validate XML using commonly available Java tools, and I compared those methods to more traditional ad hoc methods.

How-To XML & Java: Read the whole series!



This month, I wish to carry the thread further -- parsing and validating are fine as far as they go, but they don't go very far. The problem at hand typically involves doing something with the parsed information. But what if you don't understand the tags used to generate the information? Come walk with me a bit farther along the border between Java and XML, and I'll show you how to use Java to solve that problem, too.

XML tags: What to do?

Let's proceed straight to the heart of the matter. The feature of XML we need to address is its ability to define new tags. A tag in XML says something about the meaning of the content (and about the other tags) it associates with. Because the set of tags in XML is open (unlike HTML, where the set is closed), it's impossible to build an application that handles the entire tag set right out of the box. This introduces a bit of uncertainty into the process. What exactly do you do with tags you don't understand?

  • Applications can ignore novel tags. This was the approach typically taken by browser vendors during the height of the browser wars. Leading browser vendors merrily defined new tags with each release of their product, and each browser distribution quietly ignored those tags it didn't understand. This approach is safe but not very satisfactory.
  • Organizations can standardize on a set of tags. This approach cleverly sidesteps the entire problem. You define a set of standard tags and a document type definition (DTD), and then reject any XML that doesn't fit the mold. This is actually the right solution for many problems. Sales orders, for example, fit a well-defined pattern. Nothing is gained by allowing e-commerce partners to define new tags (at least without constraint -- the case could be made for the applicability of certain well-constrained tag definitions, such as macros). Unfortunately, not all applications -- XML browsers, for example -- fit within this box.
  • Applications can try to figure out what to do with novel tags. Browsers and content-presentation tools as well as other general-purpose XML tools must behave correctly in the presence of novel but valid tags. There are several ways to solve this problem. The Extensible Stylesheet Language (XSL) is one such attempt. XSL provides a translation toolkit, which allows you to define a mapping or translation from a tag set you don't understand to a tag set you do understand (for example, XML to HTML). This solution, however, has its own limitations.
  • You can build a new framework. While each of the solutions above has its place, we'll explore another solution altogether. Our solution calls for enabling the browser or XML tool to look for and download code designed to handle the novel tags and then integrate that code into the application. To do this, we'll build a new framework.


Before we can get down to the business of building our solution, we need to understand a little more about XML. In particular, we need to understand how to manipulate XML within an application. We need to understand the Document Object Model (DOM).

The DOM

The DOM is a platform-independent, programming-language-neutral API that allows programs to access and modify the content and the structure of XML documents from within applications.

At its core, the DOM defines a family of types that represent all the objects that make up an XML document: elements, attributes, entity references, comments, textual data, processing instructions, and the rest. (I use the word object throughout this article to loosely refer to the building-blocks of an XML document.) The DOM, originally envisioned as living inside a browser, has turned out to have a much broader impact. It is also worth noting that the DOM isn't specific to XML. It applies equally well to HTML.

To comprehend the DOM you need to remember that a key characteristic of XML is the notion that many documents can be represented as a hierarchical structure of content and markup. The code below, for example, represents a valid XML document:

<html>
 <head>
  <title>
   This is the title.
  </title>
 </head>
 <body>
  <h1>
   This is a headline.
  </h1>
  This is the body.
  And this is more of the body.
 </body>
</html>


I don't want to provide you with an XML primer, but I do want to make one point clear: A key requirement of XML (and HTML) is that tags must nest -- they may not overlap. Therefore <one><two></two></one> counts as valid XML but <one><two></one></two> does not. As a consequence, well-formed XML documents map cleanly to a tree-like data structure. Next, we can transform the document above into the tree in Figure 1, below.

Figure 1. An XML tree



The DOM provides the mechanism we need to dynamically interact with the elements and content in an XML document. Consider the tree in Figure 1. I have mapped the tags (elements, in DOM parlance) that make up our initial XML document to the nodes of the tree in Figure 1.

Each tag has meaning within the context of the enclosing tags and the document as a whole. Consider the tags again. These tags clearly define presentation-related elements within a browser. As such, they have well-understood behavior associated with them. We expect the browser to know how to draw them within the browser window. The code that implements the behavior is present within the browser.

Resources
  • Related reading