Processing Xml with Java

  • Authors:
  • Elliote Rusty Harold

  • Affiliations:
  • -

  • Venue:
  • Processing Xml with Java
  • Year:
  • 2002

Quantified Score

Hi-index 0.00

Visualization

Abstract

From the Book:One night five developers, all of whom wore very thick glasses and had recently been hired byElephants, Inc., the world's largest purveyor of elephants and elephant supplies, were familiarizingthemselves with the company's order processing system when they stumbled into a directoryfull of XML documents on the main server. "What's this?" the team leader asked excitedly.None of them had ever heard of XML before so they decided to split up the files between themand try to figure out just what this strange and wondrous new technology actually was.The first developer, who specialized in optimizing Oracle databases, printed out a stack of FMPXMLRESULTdocuments generated by the FileMaker database where all the orders werestored, and began pouring over them. "So this is XML! Why, it's nothing novel. As anyone cansee who's able, an XML document is nothing but a table!""What do you mean, a table?" replied the second programmer, well versed in object orientedtheory and occupied with a collection of XMI documents that encoded UML diagrams for thesystem. "Even a Visual Basic programmer could see that XML documents aren't tables. Duplicatesaren't allowed in a table relation, unless this is truly some strange mutation. Classes andobjects is what these document are. Indeed, it should be obvious on the very first pass. An XMLdocument is an object and a DTD is a class.""Objects? A strange kind of object, indeed!" said the third developer, a web designer of somerenown, who had loaded the XHTML user documentation for the order processing system intoMozilla. "I don't see any types at all. If you think this is an object, then it's your software Irefuse to install. But with all thosestylesheets there, it should be clear to anyone not sedated,that XML is just HTML updated!""HTML? You must be joking" said the fourth, a computer science professor on sabbatical fromMIT, who was engrossed in an XSLT stylesheet that validated all the other documents against aSchematron schema. "Look at the clean nesting of hierarchical structures, each tag matching itspartner as it should. I've never seen HTML that looks this good. What we have here is Sexpressions,which is certainly nothing new. Babbage invented this back in 1882!""S expressions?" queried the technical writer, who was occupied with documentation for theproject written in DocBook. "Maybe that means something to those in your learned profession.But to me, this looks just like a FrameMaker MIF file. However, locating the GUI does seem tobe taking me awhile."And so they argued into the night, none of them willing to give an inch, all of them presentingstill more examples to prove their points, none of them bothering to look at the others' examples.Indeed, they're probably still arguing today. You can even hear their shouts from time totime on xml-dev. Their mistake, of course, was in trying to force XML into the patterns of technologiesthey were already familiar with rather than taking it on its own terms. XML can storedata, but it is not a database. XML can serialize objects, but an XML document is not an object.Web pages can be written in XML, but XML is not HTML. Functional (and other) programminglanguages can be written in XML, but XML is not a programming language. Books arewritten in XML, but that doesn't make XML desktop publishing software.XML is something truly new that has not been seen before in the world of computing. Therehave been precursors to it, and there are always fanatics who insist on seeing XML throughdatabase (or object, or functional, or S-expression) colored glasses. But XML is none of thesethings. It is something genuinely unique and new in the world of computing; and it can only beunderstood when you're willing to accept it on its own terms, rather than forcing it into yesterday'spigeon holes.There are a lot of tools, APIs, and applications in the world that try to pretend XML is somethingmore familiar to programmers; that it's just a funny kind of database, or just like an object,or just like remote procedure calls. These APIs are occasionally useful in very restricted andpredictable environments. However, they are not suitable for processing XML in its most generalformat. They work well in their limited domains, but they fail when presented with XMLthat steps outside the artificial boundaries they've defined. XML was designed to be extensible,but it's a sad fact that many of the tools designed for XML aren't nearly as extensible as XMLitself.This book is going to show you how to handle XML in its full generality. It pulls no punches. Itdoes not pretend that XML is anything except XML, and it shows you how to design your programsso that they handle real XML in all its messiness: valid and invalid, mixed and unmixed,typed and untyped, and both all and none of these at the same time. To that end, this book focuseson those APIs that don't try to hide the XML. In particular, there are three major JavaAPIs that correctly model XML, as opposed to modeling a particular class of XML documentsor some narrow subset of XML. These are:SAX, the Simple API for XMLDOM, the Document Object ModelJDOM, a Java native APIThese APIs are the core of this book. In addition I cover a number of preliminaries and supplementsto the basic APIs including:XML syntaxDTDs, schemas, and validityXPathXSLT and the TrAX APIJAXP, a combination of SAX, DOM, and TrAX with a few factory classesAnd, since we're going to need a few examples of XML applications to demonstrate the APIswith, I also cover XML-RPC, SOAP, and RSS in some detail. However, the techniques thisbook teaches are hardly limited to just those three applications.Who You AreThis book is written for experienced Java programmers who want to integrate XML into theirsystems. Java is the ideal language for processing XML documents. Its strong Unicode supportin particular made it the preferred language for many early implementers. Consequently, moreXML tools have been written in Java than in any other language. More open source XML toolsare written in Java than in any other language. More programmers process XML in Java than inany other language.Processing XML with Java™ will teach you how to:Save XML documents from applications written in JavaRead XML documents produced by other programsSearch, query, and update XML documentsConvert legacy flat data into hierarchical XMLCommunicate with network servers that send and receive XML dataValidate documents against DTDs, schemas, and business rulesCombine functional XSLT transforms with traditional imperative Java codeThis book is meant for Java programmers who need to do anything with XML. It teaches thefundamentals and advanced topics, leaving nothing out. It is a comprehensive course in processingXML with Java that takes developers from little knowledge of XML to designing sophisticatedXML applications and parsing complicated documents. The examples cover a wide rangeof possible uses including file formats, data exchange, document transformation, database integration,and more.What You Need to KnowThis is not an introductory book with respect to either Java or XML. I assume you have substantialprior experience with Java and preferably some experience with XML. On the Java side, Iwill freely use advanced features of the language and its class library without explanation orapology. Among other things, I assume you are thoroughly familiar with:Object oriented programming including inheritance and polymorphismPackages and the CLASSPATH. You should not be surprised by classes that do not havemain() methods or that are not in the default package.I/O including streams, readers, and writers. You should understand that System.out is ahorrible example of what really goes on in Java programs.The Java Collections API including hash tables, maps, sets, iterators, and lists.In addition, in one or two places in this book I'm going to use some SQL and JDBC. However,these sections are relatively independent of the rest of the book; and chances are if you aren't alreadyfamiliar with SQL, then you don't need the material in these sections anyway.What You Need to HaveXML is deliberately architecture, platform, operating system, GUI, and language agnostic (infact, more so than Java). It works equally well on Mac OS, Windows, Linux, OS/2, various flavorsof Unix, and more. It can be processed with Python, C++, Haskell, ECMAScript, C#, Perl,Visual Basic, Ruby, and of course Java. No byte order issues need concern you if you switch betweenPowerPC, X86, or other architectures. Almost everything in this book should workequally well on any platform that's capable of running Java.Most of the material in this book is relatively independent of the specific Java version. Java 1.4bundles SAX, DOM, and a few other useful classes into the core JDK. However, these are easilyinstalled in earlier JVMs as open source libraries from the Apache XML Project and othervendors. For the most part, I used Java 1.3 and 1.4 when testing the examples; and it's possiblethat a few classes and methods have been used that are not available in earlier versions. In mostcases, it should be fairly obvious how to backport them. All of the basic XML APIs exceptTrAX should work in Java 1.1 and later. TrAX requires Java 1.2 or later.How to Use This BookThis book is organized as an advanced tutorial that can also serve as a solid and comprehensivereference. The first chapter covers the bare minimum material needed to start working withXML, though for the most part this is intended more as a review for readers who've alreadyread other, more basic books than as a comprehensive introduction. The second chapter introducesRSS, XML-RPC, and SOAP, the XML applications we'll be using for examples in therest of the book. This is followed by two chapters on generating XML from your own programs(a subject which is all too often presented as a lot more complicated than it actually is). The firstcovers generating XML directly from code. The second covers converting legacy data in otherformats to XML. The remaining bulk of the book is devoted to the major APIs for processingXML:The event based SAX APIThe tree-based DOM APIThe tree-based JDOM APIXPath APIs for searching XML documentsThe TrAX API for XSLT processingFinally, the book finishes with an appendix providing quick references to the main APIs.If you have limited experience with XML, I suggest you read at least the first five chapters inorder. From that point forward, if you have a particular API preference, you may begin with thepart covering the major API you're interested in:Chapters 6-8 cover SAXChapters 9-13 cover DOMChapters 14 and 15 cover JDOMOnce you're comfortable with one or more of these APIs, you can read Chapters 16 and 17 onXPath and XSLT. However, those APIs and chapters do require some knowledge of at least oneof the three major APIs.