2006 - pro xml development with java technology (apress)

449 345 0
2006 - pro xml development with java technology (apress)

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Pro XML Development with Java TM Technology ■■■ Ajay Vohra and Deepak Vohra Vohra_706-0FRONT.fm Page i Tuesday, August 15, 2006 9:01 AM Pro XML Development with Java TM 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 1 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. 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. Vohra_706-0FRONT.fm Page ii Tuesday, August 15, 2006 9:01 AM v 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 Object Binding with JAXB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 ■CHAPTER 7 Binding with XMLBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 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 Converting XML to Spreadsheet, and Vice Versa . . . . . . . . . . . . . . 289 ■CHAPTER 12 Converting XML to PDF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 Vohra_706-0FRONT.fm Page v Tuesday, August 15, 2006 9:01 AM vi PART 6 ■ ■ ■ Web Applications and Services ■CHAPTER 13 Building Web Applications with Ajax . . . . . . . . . . . . . . . . . . . . . . . . . 329 ■CHAPTER 14 Building XML-Based Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . 353 ■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417 Vohra_706-0FRONT.fm Page vi Tuesday, August 15, 2006 9:01 AM vii Contents About the Authors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xv About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvi Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii PART 1 ■ ■ ■ Parsing, Validating, and Addressing ■CHAPTER 1 Introducing XML and Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Scope of This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Overview of This Book’s Contents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 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 Vohra_706-0FRONT.fm Page vii Tuesday, August 15, 2006 9:01 AM viii ■CONTENTS Introducing the Eclipse IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Creating a Java Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Setting the Build Path. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Creating a Java Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Creating a Java Class. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Running a Java Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 Importing a Java Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 ■CHAPTER 2 Parsing XML Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Objectives of Parsing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Overview of Parsing Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 DOM Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 Push Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Pull Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Comparing the Parsing Approaches . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Setting Up an Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Example XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 J2SE, Packages, and Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 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 Introducing Schema Validation . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Schema Validation APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Configuring JAXP Parsers for Schema Validation . . . . . . . . . . . . . . . 66 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Vohra_706-0FRONT.fm Page viii Tuesday, August 15, 2006 9:01 AM ■CONTENTS ix JAXP 1.3 DOM Parser API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Create a DOM Parser Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Configure a Factory for Validation. . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Create a DOM Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Configure a Parser for Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Validate Using the Parser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Complete DOM API Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 JAXP 1.3 SAX Parser API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Create a SAX Parser Factory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Configure the Factory for Validation . . . . . . . . . . . . . . . . . . . . . . . . . . 76 Create a SAX Parser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Configure the Parser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77 Validate Using the Parser. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 Complete SAX API Validator Example. . . . . . . . . . . . . . . . . . . . . . . . . 78 JAXP 1.3 Validation API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Create a Validator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Set an Error Handler. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Validate the XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 Complete JAXP 1.3 Validator Example. . . . . . . . . . . . . . . . . . . . . . . . 81 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 ■CHAPTER 4 Addressing with XPath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Understanding XPath Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 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 Vohra_706-0FRONT.fm Page ix Tuesday, August 15, 2006 9:01 AM x ■CONTENTS ■CHAPTER 5 Transforming with XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Overview of XSLT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 XSLT Processing Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 XSLT Syntax and Semantics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 JAXP 1.3 Transformation APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 TrAX Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 Transforming Identically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Removing Duplicates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 Sorting Elements. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Converting to HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 Merging Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 Obtaining Node Values with XPath . . . . . . . . . . . . . . . . . . . . . . . . . . 131 Filtering Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 Copying Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Creating Elements and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 Adding Indentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 PART 2 ■ ■ ■ Object Bindings ■CHAPTER 6 Object Binding with JAXB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 JAXB 1.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 XML Schema Binding to Java Representation. . . . . . . . . . . . . . . . . 141 Example Use Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 Downloading and Installing the Software . . . . . . . . . . . . . . . . . . . . 147 Creating and Configuring the Eclipse Project . . . . . . . . . . . . . . . . . 147 Binding the Catalog Schema to Java Classes . . . . . . . . . . . . . . . . . 149 Marshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 Unmarshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 Customizing JAXB Bindings. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 Global Binding Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Schema Binding Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Datatype Binding Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Class Binding Declarations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Property Binding Declarations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Vohra_706-0FRONT.fm Page x Tuesday, August 15, 2006 9:01 AM ■CONTENTS xi JAXB 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Architecture. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 XML Schema Binding to Java Representation. . . . . . . . . . . . . . . . . 165 Example Use Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 Downloading and Installing Software. . . . . . . . . . . . . . . . . . . . . . . . 169 Creating and Configuring Eclipse Project. . . . . . . . . . . . . . . . . . . . . 169 Binding Catalog Schema to Java Classes . . . . . . . . . . . . . . . . . . . . 171 Marshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174 Unmarshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . 177 Binding Java Classes to XML Schema . . . . . . . . . . . . . . . . . . . . . . . 180 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 ■CHAPTER 7 Binding with XMLBeans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 Compiling an XML Schema . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Customizing XMLBeans Bindings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Marshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 Unmarshaling an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200 Traversing an XML Document with the XmlCursor API . . . . . . . . . . . . . 203 Positioning the Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204 Adding an Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206 Selecting Nodes with XPath. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 Querying an XML Document with XQuery . . . . . . . . . . . . . . . . . . . . 208 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 PART 3 ■ ■ ■ XML and Databases ■CHAPTER 8 Storing XML in Native XML Databases: Xindice . . . . . . . . . 215 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Simple Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217 Installing the Xindice Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218 Configuring Xindice with the JBoss Server . . . . . . . . . . . . . . . . . . . 219 Creating an Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 Vohra_706-0FRONT.fm Page xi Tuesday, August 15, 2006 9:01 AM xii ■CONTENTS Using the Xindice Command-line Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Command Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 Command Configuration in Eclipse. . . . . . . . . . . . . . . . . . . . . . . . . . 223 Xindice Command Examples. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225 Deleting a Xindice Collection. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 Using Xindice with the XML:DB API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Creating a Collection in the Xindice Database. . . . . . . . . . . . . . . . . 237 Adding an XML Document to the Xindice Database . . . . . . . . . . . . 239 Retrieving an XML Document from the Xindice Database . . . . . . . 239 Querying the Xindice Database Using XPath . . . . . . . . . . . . . . . . . . 240 Modifying the Document Using XUpdate . . . . . . . . . . . . . . . . . . . . . 240 Deleting an XML Document. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 ■CHAPTER 9 Storing XML in Relational Databases . . . . . . . . . . . . . . . . . . . 249 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 Installing the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 Setting Up the Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251 Selecting a Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 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 Loading and Saving with the DOM Level 3 API . . . . . . . . . . 267 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Introducing the Load API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Introducing the Save API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268 Comparing JAXP’s DocumentBuilder and Transformer APIs . . . . . 269 Creating an Eclipse Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 Loading an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 Saving an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 Filtering an XML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 Vohra_706-0FRONT.fm Page xii Tuesday, August 15, 2006 9:01 AM [...]... create a Java project and how to create a Java application within a Java project Creating a Java Project To create a Java project in Eclipse, select File ➤ New ➤ Project In the New Project dialog box, select Java Project, and then click Next, as shown in Figure 1-1 19 Vohra_70 6-0 C01.fm Page 20 Wednesday, June 28, 2006 6:27 AM 20 CHAPTER 1 ■ INTRODUCING XML AND JAVA Figure 1-1 Selecting the New Project... XML 1.0 specification at http://www.w3.org/TR/2004/ REC -xml- 20040204/#sec-well-formed 3 http:/ /java. sun.com/javaee/ 3 Vohra_70 6-0 C01.fm Page 4 Wednesday, June 28, 2006 6:27 AM 4 CHAPTER 1 ■ INTRODUCING XML AND JAVA • XSL Transformations (XSLT) 1.0 (http://www.w3.org/TR/xslt) describes a language for transforming an XML document into other XML or non -XML documents Transformation API for XML (TrAX) within... subset of XMLrelated 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 XML document and describes partial rules for processing well-formed2 documents Java API for XML Processing... among disparate applications XML defines precise syntactic rules for what constitutes a well-formed 10 XML 1.0 is a W3C Recommendation (http://www.w3.org/TR/2004/REC -xml- 20040204/), and XML 1.1 is a W3C Recommendation (http://www.w3.org/TR /xml1 1/) 5 Vohra_70 6-0 C01.fm Page 6 Wednesday, June 28, 2006 6:27 AM 6 CHAPTER 1 ■ INTRODUCING XML AND JAVA XML document This primer is a non-normative discussion of... Wizard On the Create a Java Project screen, specify a project name, such as Chapter1 In the Project Layout section, select Create Separate Source and Output Folders, and click Next, as shown in Figure 1-2 Vohra_70 6-0 C01.fm Page 21 Wednesday, June 28, 2006 6:27 AM CHAPTER 1 ■ INTRODUCING XML AND JAVA Figure 1-2 Creating a Java project On the Java Settings screen, add the required project libraries under... incrementally as we explain various XML Schema constructs The example schema will define a structure for the example XML document shown in Listing 1-2 Listing 1-2 Example XML Document < ?xml version='1.0' encoding='UTF-8' ?> Java and XML Narayanan Jayaratchagan 12 See XML Schema Part 1: Structures... libraries under the Libraries tab, and click Finish, as shown in Figure 1-3 21 Vohra_70 6-0 C01.fm Page 22 Wednesday, June 28, 2006 6:27 AM 22 CHAPTER 1 ■ INTRODUCING XML AND JAVA Figure 1-3 Accessing the Java Settings screen This adds a Java project to the Package Explorer in Eclipse, as shown in Figure 1-4 Figure 1-4 Viewing the Java project in the Package Explorer ... 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... (http://www.w3.org/TR/xmlschema-1/) and XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/) for more information 11 Vohra_70 6-0 C01.fm Page 12 Wednesday, June 28, 2006 6:27 AM 12 CHAPTER 1 ■ INTRODUCING XML AND JAVA Schema Declarations The root element of a schema is schema, and it is defined in the XML Schema namespace xmlns:xsd="http://www.w3.org/2001/XMLSchema"... document with its root element is as follows: Built-in Datatypes The XML Schema language has 44 built-in simple types that are specified in XML Schema Part 2: Datatypes (http://www.w3.org/TR/xmlschema-2/) These datatypes of course belong to the XML Schema namespace, so we will use them with the xsd: prefix, as in xsd:string Table 1-1 . Pro XML Development with Java TM Technology ■■■ Ajay Vohra and Deepak Vohra Vohra_70 6-0 FRONT.fm Page i Tuesday, August 15, 2006 9:01 AM Pro XML Development with Java TM Technology Copyright. 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/ Vohra_70 6-0 C01.fm. storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 97 8-1 -5 905 9-7 0 6-4 ISBN-10 (pbk): 1-5 905 9-7 0 6-0 Trademarked names may appear

Ngày đăng: 28/04/2014, 15:50

Mục lục

  • Pro XML Development with JavaTM Technology

    • Table of Content

    • PART 1 Parsing, Validating, and Addressing

      • Chapter 1 Introducing XML and Java

      • Chapter 2 Parsing XML Documents

      • Chapter 3 Introducing Schema Validation

      • Chapter 4 Addressing with XPath

      • Chapter 5 Transforming with XSLT

      • PART 2 Object Bindings

        • Chapter 6 Object Binding with JAXB

        • Chapter 7 Binding with XMLBeans

        • PART 3 XML and Databases

          • Chapter 8 Storing XML in Native XML Databases: Xindice

          • Chapter 9 Storing XML in Relational Databases

          • PART 4 DOM Level 3.0

            • Chapter 10 Loading and Saving with the DOM Level 3 API

            • PART 5 Utilities

              • Chapter 11 Converting XML to Spreadsheet, and Vice Versa

              • Chapter 12 Converting XML to PDF

              • PART 6 Web Applications and Services

                • Chapter 13 Building Web Applications with Ajax

                • Chapter 14 Building XML-Based Web Services

                • Index

Tài liệu cùng người dùng

Tài liệu liên quan