Table Of ContentJava & XML, 2nd Edition
Brett McLaughlin
Publisher: O'Reilly
Second Edition September 2001
ISBN: 0-596-00197-5, 528 pages
New chapters on Advanced SAX, Advanced DOM, SOAP and data binding, as well as new
examples throughout, bring the second edition of Java & XML thoroughly up to date. Except
for a concise introduction to XML basics, the book focuses entirely on using XML from Java
applications. It's a worthy companion for Java developers working with XML or involved in
messaging, web services, or the new peer-to-peer movement.
Table of Contents
Preface ..................................................... 1
Organization ................................................. 1
Who Should Read This Book? ..................................... 4
Software and Versions .......................................... 4
Conventions Used in This Book .................................... 5
Comments and Questions ........................................ 5
Acknowledgments ............................................. 6
1. Introduction ................................................ 8
1.1 XML Matters .............................................. 8
1.2 What's Important? ........................................... 10
1.3 The Essentials ............................................. 11
1.4 What's Next? .............................................. 14
2. Nuts and Bolts .............................................. 15
2.1 The Basics ............................................... 15
2.2 Constraints ............................................... 24
2.3 Transformations ............................................ 31
2.4 And More... ............................................... 38
2.5 What's Next? .............................................. 38
3. SAX ...................................................... 39
3.1 Getting Prepared ............................................ 39
3.2 SAX Readers .............................................. 41
3.3 Content Handlers ........................................... 47
3.4 Error Handlers ............................................. 60
3.5 Gotcha! ................................................. 65
3.6 What's Next? .............................................. 68
4. Advanced SAX .............................................. 69
4.1 Properties and Features ....................................... 69
4.2 More Handlers ............................................. 75
4.3 Filters and Writers .......................................... 80
4.4 Even More Handlers ......................................... 86
4.5 Gotcha! ................................................. 90
4.6 What's Next? .............................................. 92
5. DOM ..................................................... 93
5.1 The Document Object Model ................................... 93
5.2 Serialization .............................................. 97
5.3 Mutability ................................................ 108
5.4 Gotcha! ................................................. 109
5.5 What's Next? .............................................. 110
6. Advanced DOM ............................................. 111
6.1 Changes ................................................. 111
6.2 Namespaces ............................................... 120
6.3 DOM Level 2 Modules ....................................... 124
6.4 DOM Level 3 ............................................. 136
6.5 Gotcha! ................................................. 139
6.6 What's Next? .............................................. 140
7. JDOM .................................................... 141
7.1 The Basics ............................................... 141
7.2 PropsToXML ............................................. 145
7.3 XMLProperties ............................................ 154
7.4 Is JDOM a Standard? ........................................ 164
7.5 Gotcha! ................................................. 165
7.6 What's Next? .............................................. 167
8. Advanced JDOM ............................................ 168
8.1 Helpful JDOM Internals ....................................... 168
8.2 JDOM and Factories ......................................... 172
8.3 Wrappers and Decorators ...................................... 177
8.4 Gotcha! ................................................. 188
8.5 What's Next? .............................................. 190
9. JAXP ..................................................... 191
9.1 API or Abstraction .......................................... 191
9.2 JAXP 1.0 ................................................ 192
9.3 JAXP 1.1 ................................................ 199
9.4 Gotcha! ................................................. 208
9.5 What's Next? .............................................. 209
10. Web Publishing Frameworks ................................... 210
10.1 Selecting a Framework ....................................... 211
10.2 Installation ............................................... 213
10.3 Using a Publishing Framework ................................. 217
10.4 XSP ................................................... 230
10.5 Cocoon 2.0 and Beyond ...................................... 244
10.6 What's Next? ............................................. 247
11. XML-RPC ................................................ 248
11.1 RPC Versus RMI .......................................... 248
11.2 Saying Hello ............................................. 250
11.3 Putting the Load on the Server .................................. 261
11.4 The Real World ........................................... 274
11.5 What's Next? ............................................. 277
12. SOAP .................................................... 278
12.1 Starting Out .............................................. 278
12.2 Setting Up ............................................... 281
12.3 Getting Dirty ............................................. 285
12.4 Going Further ............................................. 293
12.5 What's Next? ............................................. 300
13. Web Services ............................................... 301
13.1 Web Services ............................................. 301
13.2 UDDI .................................................. 302
13.3 WSDL ................................................. 303
13.4 Putting It All Together ....................................... 306
13.5 What's Next? ............................................. 323
14. Content Syndication .......................................... 324
14.1 The Foobar Public Library .................................... 325
14.2 mytechbooks.com .......................................... 333
14.3 Push Versus Pull ........................................... 341
14.4 What's Next? ............................................. 350
15. Data Binding ............................................... 351
15.1 First Principles ............................................ 352
15.2 Castor .................................................. 357
15.3 Zeus ................................................... 364
15.4 JAXB .................................................. 372
15.5 What's Next? ............................................. 379
16. Looking Forward ........................................... 380
16.1 XLink .................................................. 380
16.2 XPointer ................................................ 382
16.3 XML Schema Bindings ...................................... 385
16.4 And the Rest... ............................................ 386
16.5 What's Next? ............................................. 386
A. API Reference .............................................. 387
A.1 SAX 2.0 ................................................. 387
A.2 DOM Level 2 ............................................. 398
A.3 JAXP 1.1 ................................................ 404
A.4 JDOM 1.0 (Beta 7) .......................................... 410
B. SAX 2.0 Features and Properties ................................. 420
B.1 Core Features ............................................. 420
B.2 Core Properties ............................................ 421
Colophon .................................................... 423
Java & XML, 2nd Edition
Preface
When I wrote the preface to the first edition of Java & XML just over a year ago, I had no
idea what I was getting into. I made jokes about XML appearing on hats and t-shirts; yet as
I sit writing this, I'm wearing a t-shirt with "XML" emblazoned across it, and yes, I have a hat
with XML on it also (in fact, I have two!). So, the promise of XML has been recognized,
without any doubt. And that's good.
However, it has meant that more development is occurring every day, and the XML landscape
is growing at a pace I never anticipated, even in my wildest dreams. While that's great for
XML, it has made looking back at the first edition of this book somewhat depressing; why is
everything so out of date? I talked about SAX 2.0, and DOM Level 2 as twinklings in eyes.
They are now industry standard. I introduced JDOM, and now it's in JSR (Sun's Java
Specification Request process). I hadn't even looked at SOAP, UDDI, WSDL, and XML data
binding. They take up three chapters in this edition! Things have changed, to say the least.
If you're even remotely suspicious that you may have to work with XML in the next few
months, this book can help. And if you've got the first edition lying somewhere on your desk
at work right now, I invite you to browse the new one; I think you'll see that this book is still
important to you. I've thrown out all the excessive descriptions of basic concepts, condensed
the basic XML material into a single chapter, and rewritten nearly every example; I've also
added many new examples and chapters. In other words, I tried to make this an in-depth
technical book with lots of grit. It will take you beginners a little longer, as I do less
handholding, but you'll find the knowledge to be gained much greater.
Organization
This book is structured in a very particular way: the first half of the book, Chapter 1 through
Chapter 9, focuses on grounding you in XML and the core Java APIs for handling XML. For
each of the three XML manipulation APIs (SAX, DOM, and JDOM), I'll give you a chapter
on the basics, and then a chapter on more advanced concepts. Chapter 10 is a transition
chapter, starting to move up the XML "stack" a bit. It covers JAXP, which is an abstraction
layer over SAX and DOM. The remainder of the book, Chapter 11 through Chapter 15,
focuses on specific XML topics that continually are brought up at conferences and tutorials
I am involved with, and seek to get you neck-deep in using XML in your applications. These
topics include new chapters on SOAP, data binding, and an updated look at
business-to-business. Finally, there are two appendixes to wrap up the book. The summary of
this content is as follows:
Chapter 1
We will look at what all the hype is about, examine the XML alphabet soup, and
spend time discussing why XML is so important to the present and future of enterprise
development.
1
Java & XML, 2nd Edition
Chapter 2
This is a crash course in XML basics, from XML 1.0 to DTDs and XML Schema to
XSLT to Namespaces. For readers of the first edition, this is the sum total (and then
some) of all the various chapters on working with XML.
Chapter 3
The Simple API for XML (SAX), our first Java API for handling XML, is introduced
and covered in this chapter. The parsing lifecycle is detailed, and the events that can
be caught by SAX and used by developers are demonstrated.
Chapter 4
We'll push further with SAX in this chapter, covering less-used but still powerful
items in the API. You'll find out how to use XML filters to chain callback behavior,
use XML writers to output XML with SAX, and look at some of the less commonly
used SAX handlers like LexicalHandler and DeclHandler.
Chapter 5
This chapter moves on through the XML landscape to the next Java and XML API,
the DOM (Document Object Model). You'll learn DOM basics, find out what is in the
current specification (DOM Level 2), and how to read and write DOM trees.
Chapter 6
Moving on through DOM, you'll learn about the various DOM modules like Traversal,
Range, Events, CSS, and HTML. We'll also look at what the new version, DOM Level
3, offers and how to use these new features.
Chapter 7
This chapter introduces JDOM, and describes how it is similar to and different from
DOM and SAX. It covers reading and writing XML using this API.
Chapter 8
In a closer examination of JDOM, we'll look at practical applications of the API, how
JDOM can use factories with your own JDOM subclasses, and JAXP integration.
You'll also see XPath in action in tandem with JDOM.
Chapter 9
Now a full-fledged API with support for parsing and transformations, JAXP merits its
own chapter. Here, we'll look at both the 1.0 and 1.1 versions, and you'll learn how to
use this API to its fullest.
2
Java & XML, 2nd Edition
Chapter 10
This chapter looks at what a web publishing framework is, why it matters to you, and
how to choose a good one. We then cover the Apache Cocoon framework, taking an
in-depth look at its feature set and how it can be used to serve highly dynamic content
over the Web.
Chapter 11
In this chapter, we'll cover Remote Procedure Calls (RPC), its relevance in distributed
computing as compared to RMI, and how XML makes RPC a viable solution for some
problems. We'll then look at using XML-RPC Java libraries and building XML-RPC
clients and servers.
Chapter 12
In this chapter, we'll look at using configuration data in an XML format, and see why
that format is so important to cross-platform applications, particularly as it relates to
distributed systems and web services.
Chapter 13
Continuing the discussions of SOAP and web services, this chapter details two
important technologies, UDDI and WSDL.
Chapter 14
Continuing in the vein of business-to-business applications, this chapter introduces
another way for businesses to interoperate, using content syndication. You'll learn
about Rich Site Summary, building information channels, and even a little Perl.
Chapter 15
Moving up the XML "stack," this chapter covers one of the higher-level Java and
XML APIs, XML data binding. You'll learn what data binding is, how it can make
working with XML a piece of cake, and the current offerings. I'll look at three
frameworks: Castor, Zeus, and Sun's early access release of JAXB, the Java
Architecture for XML Data Binding.
Chapter 16
This chapter points out some of the interesting things coming up over the horizon, and
lets you in on some extra knowledge on each. Some of these guesses may be
completely off; others may be the next big thing.
Appendix A
This appendix details all the classes, interfaces, and methods available for use in the
SAX, DOM, JAXP, and JDOM APIs.
3
Java & XML, 2nd Edition
Appendix B
This appendix details the features and properties available to SAX 2.0 parser
implementations.
Who Should Read This Book?
This book is based on the premise that XML is quickly becoming (and to some extent has
already become) an essential part of Java programming. The chapters instruct you in the use
of XML and Java, and other than in Chapter 1, they do not focus on if you should use XML. If
you are a Java developer, you should use XML, without question. For this reason, if you are a
Java programmer, want to be a Java programmer, manage Java programmers, or are
associated with a Java project, this book is for you. If you want to advance, become a better
developer, write cleaner code, or have projects succeed on time and under budget; if you need
to access legacy data, need to distribute system components, or just want to know what the
XML hype is about, this book is for you.
I tried to make as few assumptions about you as possible; I don't believe in setting the entry
point for XML so high that it is impossible to get started. However, I also believe that if you
spent your money on this book, you want more than the basics. For this reason, I only
assumed that you know the Java language and understand some server-side programming
concepts (such as Java servlets and Enterprise JavaBeans). If you have never coded Java
before or are just getting started with the language, you may want to read Learning Javaby
Pat Niemeyer and Jonathan Knudsen (O'Reilly) before starting this book. I do not assume that
you know anything about XML, and start with the basics. However, I do assume that you are
willing to work hard and learn quickly; for this reason we move rapidly through the basics so
that the bulk of the book can deal with advanced concepts. Material is not repeated unless
appropriate, so you may need to reread previous sections or flip back and forth as we use
previously covered concepts in later chapters. If you know some Java, want to learn XML,
and are prepared to enter some example code into your favorite editor, you should be able to
get through this book without any real problem.
Software and Versions
This book covers XML 1.0 and the various XML vocabularies in their latest form as of July
of 2001. Because various XML specifications covered are not final, there may be minor
inconsistencies between printed publications of this book and the current version of the
specification in question.
All the Java code used is based on the Java 1.2 platform. If you're not using Java 1.2 by now,
start to work to get there; the collections classes alone are worth it. The Apache Xerces parser,
Apache Xalan processor, Apache SOAP library, and Apache FOP libraries were the latest
stable versions available as of June of 2000, and the Apache Cocoon web publishing
framework used is Version 1.8.2. The XML-RPC Java libraries used are Version 1.0 beta 4.
All software used is freely available and can be obtained online from http://java.sun.com/,
http://xml.apache.org/, and http://www.xml-rpc.com/.
The source for the examples in this book is contained completely within the book itself. Both
source and binary forms of all examples (including extensive Javadoc not necessarily
included in the text) are available online from http://www.oreilly.com/catalog/javaxml2/ and
4
Java & XML, 2nd Edition
http://www.newinstance.com/. All of the examples that could run as servlets, or be converted
to run as servlets, can be viewed and used online at http://www.newinstance.com/.
Conventions Used in This Book
The following font conventions are used in this book.
Italic is used for:
• Unix pathnames, filenames, and program names
• Internet addresses, such as domain names and URLs
• New terms where they are defined
Boldface is used for:
• Names of GUI items: window names, buttons, menu choices, etc.
Constant Width is used for:
• Command lines and options that should be typed verbatim
• Names and keywords in Java programs, including method names, variable names, and
class names
• XML element names and tags, attribute names, and other XML constructs that appear
as they would within an XML document
Comments and Questions
Please address comments and questions concerning this book to the publisher:
O'Reilly & Associates, Inc.
101 Morris Street
Sebastopol, CA 95472
(800) 998-9938 (in the U.S. or Canada)
(707) 829-0515 (international or local)
(707) 829-0104 (fax)
You can also send us messages electronically. To be put on the mailing list or request a
catalog, send email to:
info@oreilly.com
To ask technical questions or comment on the book, send email to:
bookquestions@oreilly.com
We have a web site for the book, where we'll list examples, errata, and any plans for future
editions. You can access this page at:
http://www.oreilly.com/catalog/javaxml2/
5