Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 428 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
428
Dung lượng
5,17 MB
Nội dung
Java & 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 Organization Who Should Read This Book? Software and Versions Conventions Used in This Book Comments and Questions Acknowledgments 1 4 5 Introduction 1.1 XML Matters 1.2 What's Important? 1.3 The Essentials 1.4 What's Next? 8 10 11 14 Nuts and Bolts 2.1 The Basics 2.2 Constraints 2.3 Transformations 2.4 And More 2.5 What's Next? 15 15 24 31 38 38 SAX 3.1 Getting Prepared 3.2 SAX Readers 3.3 Content Handlers 3.4 Error Handlers 3.5 Gotcha! 3.6 What's Next? 39 39 41 47 60 65 68 Advanced SAX 4.1 Properties and Features 4.2 More Handlers 4.3 Filters and Writers 4.4 Even More Handlers 4.5 Gotcha! 4.6 What's Next? 69 69 75 80 86 90 92 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 Advanced DOM 6.1 Changes 6.2 Namespaces 6.3 DOM Level Modules 6.4 DOM Level 6.5 Gotcha! 6.6 What's Next? 111 111 120 124 136 139 140 JDOM 7.1 The Basics 7.2 PropsToXML 7.3 XMLProperties 7.4 Is JDOM a Standard? 7.5 Gotcha! 7.6 What's Next? 141 141 145 154 164 165 167 Advanced JDOM 8.1 Helpful JDOM Internals 8.2 JDOM and Factories 8.3 Wrappers and Decorators 8.4 Gotcha! 8.5 What's Next? 168 168 172 177 188 190 JAXP 9.1 API or Abstraction 9.2 JAXP 1.0 9.3 JAXP 1.1 9.4 Gotcha! 9.5 What's Next? 191 191 192 199 208 209 10 Web Publishing Frameworks 10.1 Selecting a Framework 10.2 Installation 10.3 Using a Publishing Framework 10.4 XSP 10.5 Cocoon 2.0 and Beyond 10.6 What's Next? 210 211 213 217 230 244 247 11 XML-RPC 11.1 RPC Versus RMI 11.2 Saying Hello 11.3 Putting the Load on the Server 11.4 The Real World 11.5 What's Next? 248 248 250 261 274 277 12 SOAP 12.1 Starting Out 12.2 Setting Up 12.3 Getting Dirty 12.4 Going Further 12.5 What's Next? 278 278 281 285 293 300 13 Web Services 13.1 Web Services 13.2 UDDI 13.3 WSDL 13.4 Putting It All Together 13.5 What's Next? 301 301 302 303 306 323 14 Content Syndication 14.1 The Foobar Public Library 14.2 mytechbooks.com 14.3 Push Versus Pull 14.4 What's Next? 324 325 333 341 350 15 Data Binding 15.1 First Principles 15.2 Castor 15.3 Zeus 15.4 JAXB 15.5 What's Next? 351 352 357 364 372 379 16 Looking Forward 16.1 XLink 16.2 XPointer 16.3 XML Schema Bindings 16.4 And the Rest 16.5 What's Next? 380 380 382 385 386 386 A API Reference A.1 SAX 2.0 A.2 DOM Level A.3 JAXP 1.1 A.4 JDOM 1.0 (Beta 7) 387 387 398 404 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 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 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 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 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 Java & XML, 2nd Edition Chapter 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 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 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 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 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 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 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 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 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 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 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 not assume that you know anything about XML, and start with the basics However, I 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 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 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/ .. .Java & XML, 2nd Edition Brett McLaughlin Publisher: O''Reilly Second Edition September 2001 ISBN: 0-5 9 6-0 019 7-5 , 528 pages New chapters on Advanced SAX,... 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... lower-level APIs 10 Java & XML, 2nd Edition In this book, the main example of a high-level API is XML data binding Data binding allows for taking an XML document and providing that document as a Java