Handling XML Data
(Editor's note: If you already know how to work with XML data and you have downloaded the latest version of the xerces.jar from the Apache Group, then you may move on to the next page.)
One of the most common misunderstandings about the eXtensible Markup Language (XML) is that it is a new way of displaying data in a Web browser, akin to HTML or Cascading Style Sheets (CSS). The truth is that XML is a data-representation language, not a data-presentation language. It allows you to describe your data in a meaningful way. XML enables you to declare that "these three words constitute a heading," "this is the byline," and "this section constitutes the article body." XML allows you to declare what a particular piece of data is, rather than how that data should appear in a Web page.
Consider the following XML sample:
<title> Roll Your Own Swing-based XML Editor (Part 1 of 3)
<subtitle> Part 1 - Displaying XML with a JTree</subtitle>
<author> Kyle Gabhart</author>
This is the article body
Notice that these elements are different from standard HTML, but they look similar to HTML. This is because both HTML and XML have the same origin, SGML. The difference is that HTML has a predefined tag set, and XML's grammar is flexible, allowing you to surround data with a meaningful tag (like <author>). Also notice, the way in which every element descends from the root element (<article>) and that some elements are nested even further (<subtitle> is nested within <title>). This organization is useful for three reasons: The data is more meaningful, the data is more maintainable, and the data can be easily represented as a tree structure. This is why we will be using a JTree object to graphically display the XML data. For a more thorough introduction to XML, consult "A Technical Introduction to XML."
The Java API for XML Processing (JAXP) enables applications to parse and transform XML documents using a pure Java API that is independent of a particular XML processor implementation. It functions similar to the JDBC API, in that the developer is abstracted from the details of the underlying vendor specific component (an XML parser in the case of JAXP). The Apache Group's Xerces parser provides the most up-to-date support for the JAXP API. In order to run the code developed later in this article, you will need to download the 1.3.1 Xerces binaries for your system (2.0 is still in alpha stage) from this link. Once you have downloaded the archive, extract the xerces.jar file and place it somewhere in your classpath. You can also download the xerces.jar via a link at the end of this article.
Next, we'll learn how to use the JTree Swing component.