Pro XML Development with Java Technology
® ™ THE EXPERT’S VOICE
IN JAVA TECHNOLOGY
Supports Supports ™
Java ™ versions Java versions up to 6! up to 6! Pro
XML Development ™ with
Java Technology All the essential techniques you need to know to develop
™
powerful XML applications using Java technology!
andAjay Vohra Deepak Vohra
Pro XML Development
TMwith Java Technology
■ ■ ■ Ajay Vohra and Deepak VohraTM Pro XML Development with Java Technology Copyright © 2006 by Ajay Vohra and Deepak Vohra
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means,
electronic or mechanical, including photocopying, recording, or by any information storage or retrieval
system, without the prior written permission of the copyright owner and the publisher.ISBN-13 (pbk): 978-1-59059-706-4
ISBN-10 (pbk): 1-59059-706-0
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the
U.S. and other countries.Apress, Inc. is not affiliated with Sun Microsystems, Inc., and this book was written without endorsement
from Sun Microsystems, Inc. Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark. Lead Editor: Chris Mills Technical Reviewer: Bharath Gowda Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser,
Keir Thomas, Matt Wade Project Manager: Elizabeth Seymour Copy Edit Manager: Nicole LeClerc Copy Editor: Kim Wimpsett Assistant Production Director: Kari Brooks-Copony Senior Production Editor: Laura Cheu Compositor: Susan Glinert Stevens Proofreader: Kim Burton Indexer: Carol Burbo Artist: Susan Glinert Stevens Cover Designer: Kurt Krames Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders-ny@springer-sbm.com , or
visit http://www.springeronline.com .For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com , or visit http://www.apress.com .
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to
any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly
by the information contained in this work.
The source code for this book is available to readers at http://www.apress.com in the Source Code section.
Dedicated to our parents
Contents at a Glance About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
PART 1 Parsing, Validating, and Addressing ■ ■ ■
CHAPTER 1 Introducing XML and Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 ■
CHAPTER 2 Parsing XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 ■
CHAPTER 3 Introducing Schema Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 ■
CHAPTER 4 Addressing with XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 ■
CHAPTER 5 Transforming with XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 ■
PART 2 ■ ■ ■ Object Bindings
CHAPTER 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Object Binding with JAXB ■
CHAPTER 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Binding with XMLBeans ■
PART 3 XML and Databases ■ ■ ■
CHAPTER 8 Storing XML in Native XML Databases: Xindice . . . . . . . . . . . . . . . 215 ■
CHAPTER 9 Storing XML in Relational Databases . . . . . . . . . . . . . . . . . . . . . . . . . 249 ■
PART 4 ■ ■ ■ DOM Level 3.0
CHAPTER 10 Loading and Saving with the DOM Level 3 API . . . . . . . . . . . . . . . . 267 ■
PART 5 ■ ■ ■ Utilities CHAPTER 11 . . . . . . . . . . . . . . 289
Converting XML to Spreadsheet, and Vice Versa ■
CHAPTER 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Converting XML to PDF ■ v PART 6 Web Applications and Services ■ ■ ■
CHAPTER 13 Building Web Applications with Ajax . . . . . . . . . . . . . . . . . . . . . . . . . 329
■
CHAPTER 14 Building XML-Based Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . 353
■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
■ vi
Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
PART 1 Parsing, Validating, and Addressing ■ ■ ■
■ Scope of This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Overview of This Book’s Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
CHAPTER 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Introducing XML and Java
XML 1.0 Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
XML Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Processing Instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 DOCTYPE Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Complete Example XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Namespaces in XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
XML Schema 1.0 Primer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Schema Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Built-in Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Element Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 Complex Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Complex Content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Simple Type Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Schema Example Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 vii
viii C O N T E N T S ■
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Introducing the Eclipse IDE
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Creating a Java Project
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Setting the Build Path
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Creating a Java Package
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Creating a Java Class
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Running a Java Application
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Importing a Java Project
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Summary
CHAPTER 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Parsing XML Documents ■Objectives of Parsing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Overview of Parsing Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
DOM Approach Push Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Pull Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
. . . . . . . . . . . . . . . . . . . . . . . . . . 39 Comparing the Parsing Approaches Setting Up an Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Example XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 J2SE, Packages, and Classes Parsing with the DOM Level 3 API . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Parsing with SAX 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 JAXP Pluggability for SAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 SAX Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 SAX Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 SAX Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 SAX Parsing Steps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 SAX API Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Parsing with StAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Cursor API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 Iterator API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
CHAPTER 3 . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Introducing Schema Validation ■Schema Validation APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Configuring JAXP Parsers for Schema Validation . . . . . . . . . . . . . . . 66 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
C O N T E N T S ix ■
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 JAXP 1.3 DOM Parser API
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Create a DOM Parser Factory
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Configure a Factory for Validation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Create a DOM Parser
. . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Configure a Parser for Validation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Validate Using the Parser
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Complete DOM API Example
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 JAXP 1.3 SAX Parser API
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Create a SAX Parser Factory
. . . . . . . . . . . . . . . . . . . . . . . . . . 76 Configure the Factory for Validation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Create a SAX Parser
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Configure the Parser
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Validate Using the Parser
. . . . . . . . . . . . . . . . . . . . . . . . . 78 Complete SAX API Validator Example
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 JAXP 1.3 Validation API
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Create a Validator
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Set an Error Handler
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Validate the XML Document
. . . . . . . . . . . . . . . . . . . . . . . . 81 Complete JAXP 1.3 Validator Example
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Summary
■ Understanding XPath Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
CHAPTER 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Addressing with XPath
XPath Expression Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Datatypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Location Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Applying XPath Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 Comparing the XPath API to the DOM API . . . . . . . . . . . . . . . . . . . . . 94 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
JAXP 1.3 XPath API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Explicitly Compiling an XPath Expression . . . . . . . . . . . . . . . . . . . . . 97 Evaluating a Compiled XPath Expression . . . . . . . . . . . . . . . . . . . . . . 97 Evaluating an XPath Expression Directly . . . . . . . . . . . . . . . . . . . . . . 99 Evaluating Namespace Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 JAXP 1.3 XPath Example Application . . . . . . . . . . . . . . . . . . . . . . . . 102
JDOM XPath API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 JDOM XPath Example Application . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
x C O N T E N T S ■
CHAPTER 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Transforming with XSLT ■. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Overview of XSLT Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
XSLT Processing Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
XSLT Syntax and Semantics Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 JAXP 1.3 Transformation APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 TrAX Application Transforming Identically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Removing Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Sorting Elements Converting to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Merging Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
. . . . . . . . . . . . . . . . . . . . . . . . . . 131 Obtaining Node Values with XPath Filtering Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Copying Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
. . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Creating Elements and Attributes Adding Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
PART 2 ■ ■ ■ Object Bindings
CHAPTER 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Object Binding with JAXB ■. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Overview
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 JAXB 1.0
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Architecture
. . . . . . . . . . . . . . . . . 141
XML Schema Binding to Java Representation
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145Example Use Case . . . . . . . . . . . . . . . . . . . . 147
Downloading and Installing the Software . . . . . . . . . . . . . . . . . 147
Creating and Configuring the Eclipse Project
. . . . . . . . . . . . . . . . . 149
Binding the Catalog Schema to Java Classes
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153Marshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Unmarshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
Customizing JAXB Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Global Binding Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Schema Binding Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Datatype Binding Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Class Binding Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Property Binding Declarations
■ C O N T E N T S xi
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Configuring Xindice with the JBoss Server . . . . . . . . . . . . . . . . . . . 219
Installing the Xindice Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
CHAPTER 8 Storing XML in Native XML Databases: Xindice . . . . . . . . . 215
XML and Databases ■
PART 3 ■ ■ ■
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Positioning the Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Adding an Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Selecting Nodes with XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Querying an XML Document with XQuery . . . . . . . . . . . . . . . . . . . . 208
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Unmarshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Traversing an XML Document with the XmlCursor API . . . . . . . . . . . . . 203
Compiling an XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Customizing XMLBeans Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Marshaling an XML Document
CHAPTER 7 Binding with XMLBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
JAXB 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
■
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Binding Java Classes to XML Schema . . . . . . . . . . . . . . . . . . . . . . . 180
Unmarshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Marshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Binding Catalog Schema to Java Classes . . . . . . . . . . . . . . . . . . . . 171
Creating and Configuring Eclipse Project . . . . . . . . . . . . . . . . . . . . . 169
Downloading and Installing Software . . . . . . . . . . . . . . . . . . . . . . . . 169
Example Use Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
XML Schema Binding to Java Representation . . . . . . . . . . . . . . . . . 165
Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
Creating an Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
xii C O N T E N T S ■
. . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Using the Xindice Command-line Tool
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Command Syntax
. . . . . . . . . . . . . . . . . . . . . . . . . . 223 Command Configuration in Eclipse
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Xindice Command Examples
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Deleting a Xindice Collection
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Using Xindice with the XML:DB API
. . . . . . . . . . . . . . . . . 237
Creating a Collection in the Xindice Database
. . . . . . . . . . . . 239 Adding an XML Document to the Xindice Database
. . . . . . . 239 Retrieving an XML Document from the Xindice Database
. . . . . . . . . . . . . . . . . . 240
Querying the Xindice Database Using XPath
. . . . . . . . . . . . . . . . . . . . . 240 Modifying the Document Using XUpdate
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Deleting an XML Document
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Summary
CHAPTER 9 . . . . . . . . . . . . . . . . . . . 249
Storing XML in Relational Databases ■
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Overview Installing the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 Selecting a Database Storing an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254 Retrieving an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 Navigating an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 Complete Example Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
PART 4 ■ ■ ■ DOM Level 3.0
CHAPTER 10 . . . . . . . . . . 267
Loading and Saving with the DOM Level 3 API ■
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Overview
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Introducing the Load API
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Introducing the Save API
. . . . . 269 Comparing JAXP’s DocumentBuilder and Transformer APIs
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Creating an Eclipse Project
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Loading an XML Document
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Saving an XML Document
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Filtering an XML Document
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Summary
C O N T E N T S xiii ■
PART 5 Utilities ■ ■ ■
CHAPTER 11 . . . . . . . 289
Converting XML to Spreadsheet, and Vice Versa
■Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 289 Creating an Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290 Converting an XML Document to an Excel Spreadsheet . . . . . . . . . . . . 291 Converting an Excel Spreadsheet to an XML Document . . . . . . . . . . . . 301 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309
CHAPTER 12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Converting XML to PDF
■ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Installing the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . 313
Converting an XML Document to XSL-FO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Setting the System Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Creating a Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Creating a Transformer . . . . . . . . . . . . . . . . . . 318
Transforming the XML Document to XSL-FO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Generating a PDF Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Creating a FOP Driver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Converting XSL-FO to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Viewing the Complete Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
Summary
PART 6 Web Applications and Services ■ ■ ■
CHAPTER 13 . . . . . . . . . . . . . . . . . . . . 329
Building Web Applications with Ajax ■
What Is XMLHttpRequest? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 Installing the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 331 Configuring JBoss with the MySQL Database . . . . . . . . . . . . . . . . . . . . . 332 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 Developing an Ajax Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 Browser-Side Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Web Server–Side Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 340 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
xiv C O N T E N T S ■
■ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 353
CHAPTER 14 . . . . . . . . . . . . . . . . . . . . . . 353 Building XML-Based Web Services
Overview of Web Services Understanding the Web Services Architecture . . . . . . . . . . . . . . . . . . . . 354 Basic Web Service Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 354
. . . . . . . . . . . . . . . . . . . . . . . . . . 356 Web Service Architectural Models Example Use Case Scenarios . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359 Uploading Documents to a Project . . . . . . . . . . . . . . . . . . . . . . . . . . 359
. . . . . . . . . . . . . . . . . . . . . 360 Downloading Documents from a Project Getting Information About All Projects . . . . . . . . . . . . . . . . . . . . . . . 360 Removing Documents from a Project . . . . . . . . . . . . . . . . . . . . . . . . 360
. . . . . . . . . . . . . . . 360
Understanding the SOAP 1.1 Messaging Framework
Simple SOAP 1.1 Message Exchange . . . . . . . . . . . . . . . . . . . . . . . 360SOAP 1.1 Messaging (WS-I BP 1.1) . . . . . . . . . . . . . . . . . . . . . . . . . 362 . . . . . . . . . . . . . . . . . . . . . . . . 368
SOAP 1.2 and SOAP 1.1 Differences SOAP 1.1 Message with Attachments . . . . . . . . . . . . . . . . . . . . . . . 368 Understanding WSDL 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 WSDL 1.1 Document Structure Example WSDL 1.1 Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372 Namespace Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373 Schema Definition Schema Import . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Abstract Message Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376 Port Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 Port Type Bindings to SOAP 1.1/HTTP . . . . . . . . . . . . . . . . . . . . . . . 379 Service Port . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Using JAX-WS 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385 Installing the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386 Setting Up the wsimport Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 WSDL 1.1 to Java Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 Implementing the ProjectPortType SEI . . . . . . . . . . . . . . . . . . . . . . . 397 Building the Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 400 Deploying the Web Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402 Registering a New User . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406 Web Service Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 ■ About the Authors AJAY VOHRA is a senior solutions architect at DataSynapse ( http://www.
■
datasynapse.com). His current focus is service-oriented architecture based on grid-enabled virtualized application services. He has 15 years of software development experience, spanning diverse areas such as X Windows Toolkit, ATM networking, automatic conversion of COBOL to J2EE applications, and J2EE-based enterprise applications. He has a master’s degree in computer science from Southern Illinois University–Carbondale and an MBA from the University of Michigan Ross School of Business in Ann Arbor, Michigan. Ajay is an avid golfer and loves swimming in Lake Michigan with his family.
DEEPAK VOHRA
is an independent consultant and a founding member of
■
NuBean ( http://www.nubean.com). He has worked in the area of XML and Java programming for more than five years and is a Sun Certified Java Programmer and a Sun Certified Web Component Developer. He has a master’s degree in mechanical engineering from Southern Illinois University– Carbondale and has published original research papers in the area of fluidized bed combustion. Currently, he is working on an automated, web-based J2EE development environment for NuBean. When not programming, Deepak likes to bike and play tennis.
xv About the Technical Reviewer BHARATH GOWDA works as a technical account manager (TAM) at
■
Compuware in Michigan. In his capacity as a TAM, he is responsible for crafting development solutions based on OptimalJ in the application delivery management space. Previously, he spent most of his time building and enhancing enterprise-level J2EE solutions for organizations in the Michigan region. Bharath earned his master’s degree in computer science from the University of Southern California–Los Angeles. He lives in Ann Arbor, Michigan, with his wife, Swarupa.
xvi
Acknowledgments
irst, we would like to thank all the W3C contributors who worked on numerous XML-related Drafts,
F
Working Group Notes, and Recommendations. Second, we would like to thank all the contributors who worked on XML-related Java Specification Requests. Third, we would like to thank all the soft- ware developers who worked on creating the open source software used in this book. Fourth, we would like to thank our reviewers and editors, Bharath Gowda, Kim Wimpsett, Laura Cheu, Chris Mills, and Elizabeth Seymour.
Ajay would like to thank his mentor, Professor Kenneth J. Danhof, Ph.D., for his guidance at Southern Illinois University–Carbondale. And above all, Ajay would like to thank his wife, Pam, and their kids, Sara and Stewart, for their love and understanding during the long hours spent writing this book.
xvii
P A R T 1 ■ ■ ■
Parsing, Validating,
and AddressingC H A P T E R 1 ■ ■ ■
Introducing XML and Java
xtensible Markup Language (XML) is based on simple, platform-independent rules for representing
E
structured textual information. The platform-independent nature of XML makes it an ideal format for exchanging structured textual information among disparate applications. Therefore, at the heart of it, XML is about interoperability.
1 XML 1.0 was made a W3C Recommendation in 1998. Sun formally introduced the Java program-
ming language in 1995, and within a few years Java had cemented its status as the preferred programming and execution platform for a dizzyingly diverse set of applications. Incidentally, both Java and XML were shaped with an eye toward the Internet. Therefore, it is not surprising that most of the XML-related W3C Recommendations have inspired corresponding Java-based application programming interfaces (APIs). Some of these Java APIs are part of the Java Platform Standard Edition (J2SE) platform; others are part of various open source or proprietary endeavors. XML-related W3C Recommendations and their corresponding Java APIs are the main focus of this book.
Scope of This Book
In this book, we have two main objectives. Our first objective is to discuss a selected subset of XML- related W3C Recommendations that have inspired corresponding Java APIs. And to that end, here is a quick synopsis of the XML-related W3C Recommendations and Java APIs that we’ll cover in this book:
- XML 1.0 ( http://www.w3.org/TR/REC-xml/) describes precise rules for crafting a well-formed
2 XML document and describes partial rules for processing well-formed documents. Java API
for XML Processing (JAXP) 1.3 in J2SE 5.0 is its corresponding Java API. In addition, Streaming API for XML 1.0 (StAX) in J2SE 6.0 is relevant for processing XML documents.
- XML Schema 1.0 ( http://www.w3.org/TR/xmlschema-1/) describes a language that can be used to specify the precise structure of an XML document and constrain its contents. JAXP 1.3 in J2SE 5.0 and Java XML Architecture for XML Binding (JAXB) 2.0 in Java 2 Enterprise Edition
3 (J2EE) 5.0 are corresponding Java APIs.
- XML Path Language (XPath) 1.0 ( http://www.w3.org/TR/xpath) describes a language for addressing parts of an XML document. The XPath API within JAXP 1.3 is its corresponding Java API.
1. The World Wide Web Consortium (W3C) is dedicated to developing interoperable technologies. You can find more information about the W3C at http://www.w3.org.
2. Well-formed XML documents are defined as part of the XML 1.0 specification at http://www.w3.org/TR/2004/ REC-xml-20040204/#sec-well-formed.
3. http://java.sun.com/javaee/
3
4 C H A P T E R 1
■
I N T R O D U C I N G X M L A N D J A V A
- XSL Transformations (XSLT) 1.0 ( http://www.w3.org/TR/xslt) describes a language for trans- forming an XML document into other XML or non-XML documents. Transformation API for XML (TrAX) within JAXP 1.3 is its corresponding API.
- Document Object Model Level 3 Load and Save ( http://www.w3.org/TR/DOM-Level-3-LS/) defines a platform- and language-neutral interface for bidirectional mapping between an
- SOAP
- Web Services Description Language (WSDL) 1.1 ( http://www.w3.org/TR/wsdl) is an XML-based format for describing web service endpoints. The Java API for XML Web Services (JAX-WS 2.0) in J2EE 5.0 is its corresponding Java API.
- The XMLBeans 2.0 API, which is used for XML binding to JavaBeans. This is an alternative to JAXB 2.0 and has some pros and cons compared to JAXB 2.0.
- The XML:DB
XML document and a DOM document. The DOM Level 3 API within JAXP 1.3 is its corre- sponding API.
4
1.1 and 1.2 ( http://www.w3.org/TR/soap/) define a messaging framework for exchanging
XML content across distributed processing nodes. SOAP with Attachments API for Java (SAAJ) 1.3 is its corresponding Java API.
Our second objective is to discuss selected XML-related utility Java APIs that are useful in building interoperable enterprise software solutions. And to that end, here are the utility Java APIs discussed in this book:
5
group of APIs, which can be used to access and update XML documents stored in a native XML database.
- The Java Database Connectivity (JDBC) 4.0 API, which is useful for storing XML content within a relational database.
- The Apache POI
6 API, which is useful for transforming XML content into Microsoft Excel
7 spreadsheets.
- The Apache Formatting Objects Processor (FOP)
- Briefly explain various XML-related W3C Recommendations in simple, straightforward terms, without being imprecise.
- Discuss related Java APIs from a developer’s viewpoint, without being tedious.
9 We aim to cover all this material from a pragmatic viewpoint; by that we mean we will do
the following:
Based on the overall objectives of this book, we think this book is suitable for an intermediate- to advanced-level Java developer who understands introductory XML concepts and the J2SE 5.0 core APIs.
■
Note This book is not a comprehensive, in-depth survey of XML-related W3C Recommendations. We think all
W3C Recommendations are well written and are the best source for such comprehensive information.4. SOAP is not an acronym for anything anymore; it is just a name.
5. XML:DB APIs are part of the XML DB initiative at http://xmldb-org.sourceforge.net/xupdate/.
6. Apache POI defines pure Java APIs for manipulating Microsoft file formats ( http://jakarta.apache.org/poi/).
7. Microsoft Excel is part of Microsoft Office ( http://www.microsoft.com).
8. You can find more information about the Apache FOP project at http://xmlgraphics.apache.org/fop/.
9. PDF is a de facto standard interoperable file format from Adobe ( http://www.adobe.com).
8 API, which is useful for transforming XML content into Portable Document Format (PDF).
C H A P T E R 1 ■
I N T R O D U C I N G X M L A N D J A V A
5 Overview of This Book’s Contents
We have strived to cover a wide swath of XML-related Java APIs in this book, ranging from basic, building-block APIs used to parse XML documents to more advanced APIs used to implement interop- erable XML-based web services. This book is organized in five parts. Part 1 spans Chapters 1 through 5 and covers basics of parsing, validating, addressing, and transforming XML documents. Part 2 comprises Chapters 6 and 7 and covers the binding of XML Schema to Java types. Part 3 includes Chapters 8 and 9 and focuses on XML and databases. Part 4 consists of Chapters 10 through 12 and focuses on transforming the XML document model to other document models. Part 5 consists of Chapters 13 and 14 and focuses on XML-based web applications and web services. Here is a quick synopsis of what is in each chapter: