Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java + XML = JDOM by Jason Hunter with Brett McLaughlin Sun HQ Briefing January, 2001.

Similar presentations


Presentation on theme: "Java + XML = JDOM by Jason Hunter with Brett McLaughlin Sun HQ Briefing January, 2001."— Presentation transcript:

1 Java + XML = JDOM by Jason Hunter with Brett McLaughlin Sun HQ Briefing January, 2001

2 Introductions Jason Hunter jhunter@jdom.orgCollabNethttp://collab.nethttp://servlets.com Author of "Java Servlet Programming" (O'Reilly)

3 Introductions Brett McLaughlin brett@jdom.org Lutris Technologies http://enhydra.org http://www.newInstance.com http://www.newInstance.com Author of "Java and XML" (O'Reilly)

4 What is JDOM? JDOM is a way to represent an XML document for easy and efficient reading, manipulation, and writingJDOM is a way to represent an XML document for easy and efficient reading, manipulation, and writing –Straightforward API –Lightweight and fast –Java-optimized Despite the name similarity, it's not built on DOM or modeled after DOMDespite the name similarity, it's not built on DOM or modeled after DOM An open source project with an Apache-style licenseAn open source project with an Apache-style license –950 developers on jdom-interest (high traffic) –800 lurkers on jdom-announce (low traffic)

5 The JDOM Philosophy JDOM should be straightforward for Java programmersJDOM should be straightforward for Java programmers –Use the power of the language (Java 2) –Take advantage of method overloading, the Collections APIs, reflection, weak references –Provide conveniences like type conversions JDOM should hide the complexities of XML wherever possibleJDOM should hide the complexities of XML wherever possible –An Element has text content, not a child Text node with content –Exceptions should contain useful error messages –Give line numbers and specifics, use no SAX or DOM specifics

6 More JDOM Philosophy JDOM should integrate with DOM and SAXJDOM should integrate with DOM and SAX –Reads and writes DOM docs and SAX events –Can use any DOM or SAX parser (uses adapter model or can use JAXP) Why not use DOM:Why not use DOM: –Same API on multiple languages, defined using IDL –Foreign to the Java environment, Java programmer –Fairly heavyweight in memory Why not use SAX:Why not use SAX: –No document modification, random access, or output –Fairly steep learning curve to use correctly JDOM is to DOM/SAX as RMI is to CORBAJDOM is to DOM/SAX as RMI is to CORBA –Java optimized, plays well with the other

7 Package Structure JDOM consists of four packagesJDOM consists of four packages org.jdom org.jdom.adapters org.jdom.input org.jdom.output

8 The org.jdom Package These classes represent an XML document and XML constructs:These classes represent an XML document and XML constructs: –Attribute –CDATA –Comment –DocType –Document –Element –Entity –Namespace –ProcessingInstruction –( PartialList ) –( Verifier ) –(Assorted Exceptions)

9 The org.jdom.input Package Classes for reading XML from existing sources:Classes for reading XML from existing sources: –DOMBuilder –SAXBuilder Also, outside contributions in jdom-contrib:Also, outside contributions in jdom-contrib: –ResultSetBuilder –SpitfireBuilder

10 The org.jdom.output Package Classes for writing XML to various forms of output:Classes for writing XML to various forms of output: –DOMOutputter –SAXOutputter –XMLOutputter Also, outside contributions in jdom-contrib:Also, outside contributions in jdom-contrib: –JTreeOutputter

11 General Program Flow Normally XML Document -> SAXBuilder -> XMLOutputterNormally XML Document -> SAXBuilder -> XMLOutputter DOM Node(s) JDOM Document SAXBuilder DOMBuilder XMLOutputter SAXOutputter DOMOutputter XML Document Direct Build

12 The Document lifecycle Documents are represented by the org.jdom.Document classDocuments are represented by the org.jdom.Document class –A lightweight object holding a DocType, ProcessingInstruction s, a root Element (containing other Element s), and Comment s A document can be constructed from scratch:A document can be constructed from scratch: Or it can be constructed from a file, stream, or URL:Or it can be constructed from a file, stream, or URL: Then it can be output as XML, or SAX, or DOM:Then it can be output as XML, or SAX, or DOM: Document doc = new Document( new Element("root").setText("hi")); SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(url); XMLOutputter outputter = new XMLOutputter(); outputter.output(doc, System.out);

13 Ensuring Well-Formedness The Element constructor (and all other object constructors) check to make sure the element is legalThe Element constructor (and all other object constructors) check to make sure the element is legal –i.e. the name and content don't contain inappropriate characters The add and remove methods also check document structureThe add and remove methods also check document structure –An element may only exist at one point in the tree –Only one value can be returned by getParent() –No loops in the graph are allowed –Exactly one root element must exist

14 JDOM Namespaces Namespace prefix to URI mappings are held statically in the Namespace classNamespace prefix to URI mappings are held statically in the Namespace class They're declared in JDOM like this:They're declared in JDOM like this: They're passed as optional parameters to most element and attribute manipulation methods:They're passed as optional parameters to most element and attribute manipulation methods: List kids = element.getChildren("p", xhtml); Element kid = element.getChild("title", xhtml); Attribute height = element.getAttribute( "height", xhtml); Namespace xhtml = Namespace.getNamespace( "xhtml", "http://www.w3.org/1999/xhtml");

15 The Push to 1.0 Currently JDOM is at Beta 5Currently JDOM is at Beta 5 95% of XML vocabularies compliance95% of XML vocabularies compliance –Some work to be done for IDs and IDREFs –Discussion about Namespace re-factoring –Inline DTDs still in progress –In-memory validation and well-formedness in progress Speed and memory optimizations yet to be doneSpeed and memory optimizations yet to be done

16 Extending JDOM Some possible extensions to JDOM:Some possible extensions to JDOM: –XPath (already quite far along, and usable) –XLink/XPointer (follows XPath) –XSLT (natively, now uses Xalan) –JAXP

17 API Standardization JDOM is being submitted as a Java Specification Request (JSR)JDOM is being submitted as a Java Specification Request (JSR) –Hoping for inclusion in JAXP 1.2+ (or 2.0+) Possible standardization by OASIS or related technical ratification groupPossible standardization by OASIS or related technical ratification group

18 Get Involved Download the softwareDownload the software –http://jdom.org Read the docsRead the docs –http://jdom.org Sign up for the mailing lists (see jdom.org )Sign up for the mailing lists (see jdom.org ) –jdom-announce –jdom-interest Java and XML, by Brett McLaughlinJava and XML, by Brett McLaughlin –http://www.oreilly.com/catalog/javaxml Help improve the software!Help improve the software!


Download ppt "Java + XML = JDOM by Jason Hunter with Brett McLaughlin Sun HQ Briefing January, 2001."

Similar presentations


Ads by Google