Chapter 6. XML

Introduction

Extensible Markup Language (XML) went from a risky buzzword to a ubiquitous data format in a few short years, bringing with it an overwhelming array of tools, standards, APIs, and technologies. World Wide Web Consortium’s (W3C) publication of the initial XML recommendation in 1998 redefined the way information is represented, stored, and exchanged, and it seems that a new standard involving XML is created every month: XSL, XML Query, XPointer, XPath, XLink, XHTML, SVG, MathML, XForms, XML Encryption, SOAP, XML-RPC, WSDL, XML Schema, RelaxNG, TREX, and the list continues. Such standards are accompanied by an almost endless series of standard APIs, such as SAX and DOM. To further complicate matters, Sun Microsystems has created a number of Java APIs to handle most everything XML, including JAXP, TrAX, JAXB, JAXM, JAX-RPC, JAXR, and SAAJ. It would take years to become an expert in each of these standards, tools, and APIs, and, because of this, most programmers focus on the tip of the iceberg: SAX, DOM, a little XPath, DTD, and XML Schema. This chapter focuses on parsing and data binding; reading a XML document and creating an XML document from a set of objects.

More than a few developers have noted that parsing a simple XML document with SAX or DOM can be an unreasonably complex task, especially if one is simply attempting to create a prototype. With SAX, you need to write a series of callback methods like startElement( ) and endElement(), and in DOM, you need to write a not-trivial amount of code to traverse a Document object. It isn’t the case that you should never write a DOM or SAX parser; there are some systems that call for this level of control and complexity, and a thorough knowledge of SAX, DOM, and JDOM is essential knowledge for any developer. This chapter isn’t looking to unseat the reigning standards; instead, this chapter simply introduces you to some tools that sit on top of standard parsers and do some parsing dirty work in specific situations. A very common task in any Java program is reading in an XML document and creating a set of objects, and Commons Digester and Commons Betwixt make this task as simple as it should be.

With almost a trillion different ways to parse or create a simple XML document, you can easily overlook a tool that might save days of programming time. Commons Digester, Commons Betwixt, Zeus, Quick, Apache XMLBeans, JAXB, XStream, JiBX, Exolab Castor, and others are all projects and products that deal with the simple task of serializing objects to XML and unmarshalling XML to objects. This field is crowded, and a number of these technologies have great promises and great flaws. Just as this chapter was not written to convince you to abandon SAX and DOM, this chapter was also not written to sell Digester or Betwixt over all other options. If you don’t like what you see in this chapter, there are a variety of options, and while the abundance of choice is overwhelming, open source is about having options and choices. This chapter introduces you to two battle-tested options from Jakarta Commons—Jakarta Commons Digester and Jakarta Commons Betwixt.

Commons Digester makes the task of parsing XML exceedingly straightforward; with the Digester in hand, you can map an XML document to a series of objects using a simple set of rules and a small amount of Java code. If you use Digester with an XML rule set, it is possible to write a system that parses an XML document with three or four lines of compiled code and no hardcoded logic dealing with document structure. Commons Betwixt maps beans to XML documents and vice versa. The BeanWriter class can be used to serialize an object as an XML document, and the BeanReader can be used to read an XML document. The XML produced by Betwixt can be customized with an XML document that controls such things as whether properties are to be serialized as attributes or elements, how Collections are handled, or how property names are converted to element or attribute names.

For more information about XML data binding, pick up a copy of Brett McLaughlin’s Java & XML Data Binding (O’Reilly) or Java & XML (O’Reilly). For more information about XML, see the W3C at http://www.w3c.org. For a list of technologies, take a look at Ronald Bourret’s XML Data Binding Resources (http://www.rpbourret.com/xml/XMLDataBinding.htm).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.24.180