|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TODAY'S TOP SOA & WEBSERVICES LINKS Feature Getting Started with XML Data Interchange
Getting Started with XML Data Interchange
By: Mark Wardell
Aug. 18, 2000 12:00 AM
As software developers, you may be receiving requests for XML support, implementing XML support or using a third-party package that supports or will soon support XML data interchange. Two good reasons to support XML are:
XML data interchange is easy. In this article I'll demonstrate the addition of XML support to a preexisting application - a clone of the Minesweeper game from the Windows family of operating systems. I originally wrote the article while learning the Java Swing class libraries and grid layouts. I won't discuss those topics but will focus only on the addition of XML support. This application links with the IBM XML4J and the Sun JAXP1.0 libraries, and assumes JDK 1.2 or higher. Early adopters of any new technology expose themselves to risks. I'll identify these risks and a roadmap for minimizing your exposure. The approach is through the tried-and-true object-oriented method of encapsulation. We can also minimize exposure by deciding what parts of XML to embrace and what to temporarily ignore. Let's look at the existing standards.
XML Data Standards and Parsers
XML is read and written with a parser. At this stage parsers come in two somewhat standardized flavors: DOM (Document Object Model) and SAX (Simple API for XML). In October 1998 the W3 Consortium defined the DOM parser. The SAX was defined later outside the W3C, mostly as a result of the dissatisfaction with the DOM model. The consortium went as far as to define Java parser interfaces. However, they didn't define the actual classes for parsing, just the Java interfaces. The following is from the Sun Web site: "However, the Level 1 DOM specification is silent on the subject of how to input and output. It tells you how a DOM has to operate, but does not cover methods for reading or writing XML. As a result, you can't create a DOM from an existing XML file without going outside the DOM Level 1 specification." Potentially a vendor may provide an input method, but not an output one, and maintain DOM Level 1 compliance. Since the parser libraries for DOM are defined only in terms of required interfaces, you can't code the import statement at the top of your Java classes with any accuracy, because every vendor has different classes that implement the DOM Level 1 standardized Java interfaces. The approach taken here, and demonstrated in the sample code, emphasizes encapsulation of any vendor's Java class. This is appropriate, especially in light of the schema/DTD debate that exists over the data format. Different vendors' parsers will support schemas at different times, or not at all. If you need an XML schema in the future, it would be nice if your application's code could minimize the impact of changing the parser library.
DOM and SAX Compared
The SAX model triggers an end element event as it parses the XML and allows you to create your own data structures at that time. DOM was first and is more formally standardized by the W3C standards body. The SAX model, less rigorously standardized, is supported by many parser vendors. Be aware that any data read by a DOM parser can be read by SAX. The reverse, however, isn't always true. Remember, DOM parsing is all or nothing, while SAX implements error tolerance, throws an exception on a bad XML section and continues parsing. The sample code uses the DOM model mostly for ease of use. I like the in-memory tree for small applications. For developers concerned about program size and algorithm efficiency, further investigation of SAX is warranted.
Getting Started
Class Framework Overview
Fundamentally, the object model subsumes the lower layers via "hasa" relationships or containment. On the presentation layer the JFrame descendant "hasa" GameLogic instance, which "hasa" ParserWrapper instance. The GameLogic instance uses late binding to determine if it should instantiate a Sun or IBM parser object. You could choose a more Model-View-Controller-compliant object hierarchy, but the simple containment model is sufficient to demonstrate an XML vendor-neutral object hierarchy. The model is event-driven through the Observer/Observable technique of the JDK. Note: In this Internet-enabled age there'll most likely be a separate transport layer to the left of the persistence layer.
The Data
The game will save itself to an XML file. XML is relatively self-documenting. Define your elements as you see fit. To review the contents, open the saved game data with the XML Notepad. Note that at this time Microsoft's XML Notepad doesn't support DTDs. XML Notepad can be downloaded for free from http://msdn.microsoft.com/xml/NOTEPAD/download.asp. XML schemas and DTDs allow the data modeler to define the constraints of data without specifying the code, similar to constraints on a database. Reasons to use them will be compelling when matching standards and parsers are available. The development team with the most flexible implementation can be the first to use the new parsers. Unless you're interoperating with another vendor, you have complete control over the data. A tool such as XML Notepad is ideal for modeling your data structures. XML files are fairly self-documenting. Figure 3 shows an XML file for a game that's been in progress for four seconds; it has 10 rows and columns. The Items element has Row elements with Piece elements that have attributes of Id (the column), ShownValue, UnderlyingValue and isPlayed. A DTD would be useful to limit isPlayed to [0,1] or ['T','F'].
The Class
It's easy to get started with these interfaces. Listing 1 provides the code that would read and write the gameTime parameter to or from an in-memory DOM tree, assuming that Node n is the Parent-labeled game in Figure 3. The ParserWrapper Class provides the following:
The solution is found by allowing the ParserWrapper to call back to the GameLogic class as it walks the tree. This happens when the ParserWrapper.walkTree(..) method is called. It calls back for each Node on the tree as it's traversed. The GameLogic, in this callback, can then name the Node (see the ParserWrapper method putCurrentNodeToHashTable(String strHashKey)) for later retrieval from a private hashtable in the ParserWrapper Object using the getValue(String hashKey, String attrName) function call. At this point all XML knowledge is contained in the ParserWrapper () class and any attribute value can be accessed in a quick one-step fashion via user-defined hashkeys. The callback mechanisms described above are implemented with the JDK Observer/Observable design pattern. This could be a problem if you have a predefined persistence layer that's a descendant of any class other than java.lang.Object. If this is the case, consider implementing your framework with a Model-View-Controller design pattern. During construction of the gameLogic object, the following line of code defines the game logic as a receiver of the Sax-like events. public GameLogic(ParserWrapper AGetterSetter, String FileName )When walkTree() is called, since the GameLogic Object has added itself as an Observer, the ParserWrapper calls the update() method of the game logic: private String getHashCodeKey(int I, int J){ public void update(Observable o, Object arg) {When the game logic needs to know if a piece is already played, it's accessed simply by the GameLogic objects public method: public boolean getPlayed(int i, int j){
Selection Criteria
When schema support becomes available, development teams that have chosen a vendor-neutral path that maximizes flexibility will be rewarded. Development teams should accommodate change by double-checking the flexibility of their implementations. Essentially, development teams should ask: "What is our vendor neutrality strategy?" An interesting site on the Web that promises to help developers with vendor neutrality and parser complexity can be found at www.jdom.org. JDOM believes simpler is better.
About the Sample Code
YOUR FEEDBACK
XML JOURNAL LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING XML NEWS |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||