Upload
meghan-marshall
View
16
Download
3
Embed Size (px)
DESCRIPTION
Acknowledgments. This PowerPoint is based on slides from: Jason Hunter and Brett McLaughlin. Java + XML = JDOM. Webprogramming (WEBP) 2. Mar. 2010 By Henrik Høltzer. What is JDOM?. JDOM is a way to represent an XML document for easy and efficient reading, manipulation, and writing - PowerPoint PPT Presentation
Citation preview
This PowerPoint is based on slides from:
Jason Hunter and Brett McLaughlin
Webprogramming (WEBP)2. Mar. 2010
By Henrik Høltzer
What is JDOM?JDOM is a way to represent an XML document
for easy and efficient reading, manipulation, and writingStraightforward APILightweight and fastJava-optimized
Despite the name similarity, it's not built on DOM or modeled after DOM
An open source project with an Apache-style license950 developers on jdom-interest (high traffic)800 lurkers on jdom-announce (low traffic)
The JDOM PhilosophyJDOM should be straightforward for Java
programmersUse the power of the language (Java 2)Take advantage of method overloading, the
Collections APIs, reflection, weak referencesProvide conveniences like type conversions
JDOM should hide the complexities of XML wherever possibleAn Element has text content, not a child Text
node with contentExceptions should contain useful error
messagesGive line numbers and specifics, use no SAX or
DOM specifics
More JDOM Philosophy JDOM should integrate with DOM and SAX
Reads and writes DOM docs and SAX eventsCan use any DOM or SAX parser (uses adapter model or
can use JAXP)
Why not use DOM:Same API on multiple languages, defined using IDLForeign to the Java environment, Java programmerFairly heavyweight in memory
Why not use SAX:No document modification, random access, or outputFairly steep learning curve to use correctly
JDOM is to DOM/SAX as RMI is to CORBAJava optimized, plays well with the other
The org.jdom.input PackageClasses for reading XML from existing
sources:DOMBuilderSAXBuilder
Also, outside contributions in jdom-contrib:ResultSetBuilderSpitfireBuilder
The org.jdom.output PackageClasses for writing XML to various forms of
output:DOMOutputterSAXOutputterXMLOutputter
Also, outside contributions in jdom-contrib:JTreeOutputter
General Program FlowNormally XML Document -> SAXBuilder -> XMLOutputter
DOM Node(s)
JDOM Document
SAXBuilder
DOMBuilder
XMLOutputter
SAXOutputter
DOMOutputter
XML Document
Direct Build
The Document lifecycleDocuments are rep. by the org.jdom.Document
classA lightweight object holding a DocType, ProcessingInstructions, a root Element (containing other Elements), and Comments
A document can be constructed from scratch:
Or it can be constructed from a file, stream, or URL:
Then it can be output as XML, or SAX, or DOM:
Document doc = new Document( new Element("root").setText("courseregister"));
SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(url);
XMLOutputter outputter = new XMLOutputter(); outputter.output(doc, System.out);
Ensuring Well-FormednessThe Element constructor (and all other object
constructors) check to make sure the element is legali.e. the name and content don't contain
inappropriate characters
The add and remove methods also check document structureAn element may only exist at one point in the
treeOnly one value can be returned by getParent()
No loops in the graph are allowedExactly one root element must exist