Chapter 16: XML and Java


Overview

In this chapter, you examine how XML and Java work together. In some ways XML and Java are excellent bedfellows; they're both children of the 1990s and fit in well with modern concepts of Internet and Web-based application architectures, and they both have a solid foundation in Unicode. When it comes to the details, however, there's sometimes a mismatch: For example, the mapping from XML Schema data types to classes in the Java class library is less than perfect.

Java and XML can interact in many different ways. This chapter takes a bottom-up approach, starting with the lowest-level interfaces and working steadily upward. The most important interfaces are covered in detail elsewhere in this book, so this chapter gives more of an overview: a comparative study of the different interfaces, explaining their capabilities and their strengths and weaknesses. Its goal is to enable you to choose the right interface for the job at hand without necessarily providing a detailed reference for each class and method.

At the lowest level, Java applications can read and write lexical XML, that is, XML represented in character form with angle-bracket markup. Reading lexical XML is called parsing; writing lexical XML is called serialization (although that term is overloaded, unfortunately). The first section of the chapter discusses how a Java application can interact with a parser and a serializer.

The next level up is the construction of tree-based representations of XML documents in memory. The best-known tree model is the DOM, which is described in more detail in Chapter 13. However, DOM has come under some criticism in the Java world, and a number of attempts have been made to create better models. At least three such programs have a significant user base, namely JDOM, DOM4J, and XOM. I give a brief overview of these models to help you make an informed choice.

What DOM and these other tree models have in common is that they are generic object models: They use Java objects to represent elements, attributes, and text nodes. Wouldn't it be better to translate these generic objects into objects that reflect the semantics of the information you want to manipulate, such as customers, orders, and products? This brings us to the next level up, which is data binding: the capability to define the mapping between the XML representation of data and a Java object model.

But there's a higher level still. Rather than writing your business logic in Java, which involves moving the data between a Java representation and an XML representation, you can write the business logic in declarative languages such as XPath, XSLT, and XQuery, where the data is manipulated entirely in its native XML form. Java, however, still has a role in controlling the overall structure of the application and linking together modules written in these special-purpose languages. So in the final part of the chapter, you examine how Java relates to these declarative XML-processing languages.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net