Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 401 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
401
Dung lượng
2,43 MB
Nội dung
Java & XML, 2nd Edition 1 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 Spread by Asmodeous <asmodeous77@hotmail.com> Java & XML, 2nd Edition 2 Java & XML, 2nd Edition 3 Table of Contents Preface ___________________________________________________________7 Organization ________________________________________________ 7 Who Should Read This Book? ___________________________________9 Software and Versions ________________________________________ 9 Conventions Used in This Book ________________________________ 10 Chapter 1. Introduction ________________________________________11 1.1 XML Matters ____________________________________________ 11 1.2 What's Important?________________________________________ 12 1.3 The Essentials __________________________________________ 14 1.4 What's Next?____________________________________________ 16 Chapter 2. Nuts and Bolts ______________________________________17 2.1 The Basics _____________________________________________ 17 What's with the Space Before Your End-Slash, Brett? _______22 2.2 Constraints _____________________________________________ 26 2.3 Transformations _________________________________________ 32 2.5 What's Next?____________________________________________ 38 Chapter 3. SAX__________________________________________________39 3.1 Getting Prepared ________________________________________ 39 3.2 SAX Readers ___________________________________________ 40 3.3 Content Handlers ________________________________________ 46 3.4 Error Handlers __________________________________________ 58 3.5 Gotcha! ________________________________________________ 62 3.6 What's Next?____________________________________________ 65 Chapter 4. Advanced SAX ______________________________________67 4.1 Properties and Features ___________________________________ 67 4.2 More Handlers __________________________________________ 72 4.3 Filters and Writers________________________________________ 77 4.4 Even More Handlers ______________________________________ 83 4.5 Gotcha! ________________________________________________ 87 4.6 What's Next?____________________________________________ 89 Chapter 5. DOM _________________________________________________90 Java & XML, 2nd Edition 4 5.1 The Document Object Model _______________________________ 90 5.2 Serialization ____________________________________________ 93 5.3 Mutability______________________________________________ 104 5.4 Gotcha! _______________________________________________ 104 5.5 What's Next?___________________________________________ 106 Chapter 6. Advanced DOM ____________________________________107 6.1 Changes ______________________________________________ 107 6.2 Namespaces___________________________________________ 116 Overloaded? ___________________________________________________117 6.3 DOM Level 2 Modules ___________________________________ 119 6.4 DOM Level 3___________________________________________ 131 6.5 Gotcha! _______________________________________________ 134 6.6 What's Next?___________________________________________ 135 Chapter 7. JDOM _______________________________________________137 7.1 The Basics ____________________________________________ 137 7.2 PropsToXML___________________________________________ 140 7.3 XMLProperties _________________________________________ 149 7.4 Is JDOM a Standard? ____________________________________ 159 7.5 Gotcha! _______________________________________________ 159 7.6 What's Next?___________________________________________ 161 Chapter 8. Advanced JDOM ___________________________________162 8.1 Helpful JDOM Internals___________________________________ 162 8.2 JDOM and Factories_____________________________________ 166 8.3 Wrappers and Decorators_________________________________ 170 8.4 Gotcha! _______________________________________________ 181 8.5 What's Next?___________________________________________ 183 Chapter 9. JAXP________________________________________________184 9.1 API or Abstraction_______________________________________ 184 9.2 JAXP 1.0______________________________________________ 185 9.3 JAXP 1.1______________________________________________ 191 9.4 Gotcha! _______________________________________________ 199 9.5 What's Next?___________________________________________ 200 Chapter 10. Web Publishing Frameworks____________________202 10.1 Selecting a Framework __________________________________ 203 10.2 Installation____________________________________________ 205 Java & XML, 2nd Edition 5 10.3 Using a Publishing Framework ____________________________ 209 10.4 XSP_________________________________________________ 221 10.5 Cocoon 2.0 and Beyond _________________________________ 234 10.6 What's Next?__________________________________________ 236 Chapter 11. XML-RPC__________________________________________237 11.1 RPC Versus RMI ______________________________________ 237 11.2 Saying Hello __________________________________________ 239 11.3 Putting the Load on the Server ____________________________ 249 11.4 The Real World________________________________________ 262 11.5 What's Next?__________________________________________ 264 Chapter 12. SOAP______________________________________________265 12.1 Starting Out___________________________________________ 265 12.2 Setting Up ____________________________________________ 268 12.3 Getting Dirty __________________________________________ 271 12.4 Going Further _________________________________________ 278 12.5 What's Next?__________________________________________ 286 Chapter 13. Web Services_____________________________________288 13.1 Web Services _________________________________________ 288 13.2 UDDI ________________________________________________ 289 13.3 WSDL _______________________________________________ 290 13.4 Putting It All Together ___________________________________ 291 13.5 What's Next?__________________________________________ 307 Chapter 14. Content Syndication _____________________________308 14.1 The Foobar Public Library _______________________________ 308 14.2 mytechbooks.com______________________________________ 316 14.3 Push Versus Pull ______________________________________ 324 14.4 What's Next?__________________________________________ 332 Chapter 15. Data Binding _____________________________________333 15.1 First Principles ________________________________________ 334 15.2 Castor _______________________________________________ 338 15.3 Zeus ________________________________________________ 345 15.4 JAXB________________________________________________ 352 15.5 What's Next?__________________________________________ 358 Chapter 16. Looking Forward _________________________________360 16.1 XLink________________________________________________ 360 Java & XML, 2nd Edition 6 16.2 XPointer _____________________________________________ 361 16.3 XML Schema Bindings __________________________________ 364 16.4 And the Rest. . . _______________________________________ 365 16.5 What's Next?__________________________________________ 366 Appendix A. API Reference ___________________________________367 A.1 SAX 2.0 ______________________________________________ 367 A.2 DOM Level 2 __________________________________________ 377 A.3 JAXP 1.1 _____________________________________________ 384 A.4 JDOM 1.0 (Beta 7) ______________________________________ 389 Appendix B. SAX 2.0 Features and Properties _______________399 B.1 Core Features _________________________________________ 399 B.2 Core Properties ________________________________________ 400 Java & XML, 2nd Edition 7 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. 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 Java & XML, 2nd Edition 8 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. 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 Java & XML, 2nd Edition 9 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. 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 Java by 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 favourite 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 Java & XML, 2nd Edition 10 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 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 [...]... important concepts Example 2-1 The contents .xml document < ?xml version="1.0"?> Java and XML Contents > Java and XML ... stylesheet for Example 2-1 < ?xml version="1.0"?> ... value of the attribute, as shown here: This essentially says for the namespace http://www.oreilly.com/javaxml2, look up definitions in the schema called... Apache Xerces: http:/ /xml. apache.org IBM XML4 J: http://alphaworks.ibm.com/tech /xml4 j James Clark's XP: http://www.jclark.com /xml/ xp Oracle XML Parser: http://technet.oracle.com/tech /xml Sun Microsystems Crimson: http:/ /xml. apache.org/crimson Tim Bray's Lark and Larval: http://www.textuality.com/Lark The Mind Electric's Electric XML: http://www.themindelectric.com/products /xml/ xml.html Microsoft's MXSML... confusing) point: XML Schema, which I will talk about more in a later section, requires the schema of an XML document to be specified in a manner that looks very similar to a set of namespace declarations; see Example 2-2 Example 2-2 Referencing an XML Schema < ?xml version="1.0"?> . ora:series=" ;Java& quot;> ;Java and XML& lt;/title> <! Chapter List > <contents> <chapter title="Introduction" number="1"> <topic name=" ;XML. SYSTEM "DTD/JavaXML.dtd"> <! Java and XML Contents > <book xmlns="http://www.oreilly.com/javaxml2" xmlns:ora="http://www.oreilly.com" > <title. Handlers" /> <topic name="Gotcha!" /> <topic name="What's Next?" /> </chapter> <chapter title="DOM" number="5"> <topic