Java + XML = JDOM by Jason Hunter and Brett McLaughlin co-creators of JDOM Mountain View Java User's Group April 26, 2000.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Web architectures – Laurea Specialistica in Informatica – Università di Trento Java XML parsing.
Advertisements

1 XSLT – eXtensible Stylesheet Language Transformations Modified Slides from Dr. Sagiv.
SE 370: Programming Web Services Week 4: SOAP & NetBeans Copyright © Steven W. Johnson February 1, 2013.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
JDOM: How It Works, and How It Opened the Java Process by Jason Hunter O'Reilly Open Source Convention 2001 July, 2001.
Java + XML = JDOM by Jason Hunter and Brett McLaughlin XML DevCon November, 2000.
SAX A parser for XML Documents. XML Parsers What is an XML parser? –Software that reads and parses XML –Passes data to the invoking application –The application.
14-Jun-15 DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard SAX is an ad-hoc.
31 Signs That Technology Has Taken Over Your Life: #6. When you go into a computer store, you eavesdrop on a salesperson talking with customers -- and.
21-Jun-15 SAX (Abbreviated). 2 XML Parsers SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C standard.
Fonts and colors Times New Roman “quotes” Trebuchet "quotes" yellow blue pink green violet.
27-Jun-15 XOM. XOM Design Principles XOM (XML Object Model) is yet another XML parser, by textbook author Elliott Rusty Harold The goals of XOM, in order,
28-Jun-15 StAX Streaming API for XML. XML parser comparisons DOM is Memory intensive Read-write Typically used for documents smaller than 10 MB SAX is.
29-Jun-15 JAXB Java Architecture for XML Binding.
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
1 Web Based Programming Section 6 James King 12 August 2003.
Introduction to XML cs3505. References –I got most of this presentation from this site –O’reilly tutorials.
1 XML at a neighborhood university near you Innovation 2005 September 16, 2005 Kwok-Bun Yue University of Houston-Clear Lake.
XML for E-commerce III Helena Ahonen-Myka. In this part... n Transforming XML n Traversing XML n Web publishing frameworks.
XML eXtensible Markup Language w3c standard Why? Store and transport data Easy data exchange Create more languages WSDL (Web Service Description Language)
XP New Perspectives on XML, 2 nd Edition Tutorial 10 1 WORKING WITH THE DOCUMENT OBJECT MODEL TUTORIAL 10.
Using JavaBeans and Custom Tags in JSP Lesson 3B / Slide 1 of 37 J2EE Web Components Pre-assessment Questions 1.The _____________ attribute of a JSP page.
Java + XML = JDOM by Jason Hunter and Brett McLaughlin Enterprise Java O’Reilly Conference March, 2001.
SAX Parsing Presented by Clifford Lemoine CSC 436 Compiler Design.
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
XML Parsers Overview  Types of parsers  Using XML parsers  SAX  DOM  DOM versus SAX  Products  Conclusion.
SAX. What is SAX SAX 1.0 was released on May 11, SAX is a common, event-based API for parsing XML documents Primarily a Java API but there implementations.
Electronic Commerce COMP3210 Session 4: Designing, Building and Evaluating e-Commerce Initiatives – Part II Dr. Paul Walcott Department of Computer Science,
Working with the XML Document Object Model ©NIITeXtensible Markup Language/Lesson 7/Slide 1 of 44 Objectives In this lesson, you will learn to: *Identify.
INTRODUCTION TO JAVASCRIPT AND DOM Internet Engineering Spring 2012.
Intro to XML Originally Presented by Clifford Lemoine Modified by Box.
WEB BASED DATA TRANSFORMATION USING XML, JAVA Group members: Darius Balarashti & Matt Smith.
Consuming eXtensible Markup Language (XML) feeds.
Java + XML = JDOM by Jason Hunter with Brett McLaughlin Sun HQ Briefing January, 2001.
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
1 Tutorial 14 Validating Documents with Schemas Exploring the XML Schema Vocabulary.
Server-side Programming The combination of –HTML –JavaScript –DOM is sometimes referred to as Dynamic HTML (DHTML) Web pages that include scripting are.
XML Study-Session: Part III
This PowerPoint is based on slides from: Jason Hunter and Brett McLaughlin.
XML and SAX (A quick overview) ● What is XML? ● What are SAX and DOM? ● Using SAX.
COSC617 Project XML Tools Mark Liu Sanjay Srivastava Junping Zhang.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CS 157B: Database Management Systems II February 13 Class Meeting Department of Computer Science San Jose State University Spring 2013 Instructor: Ron.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
When we create.rtf document apart from saving the actual info the tool saves additional info like start of a paragraph, bold, size of the font.. Etc. This.
What is XML? eXtensible Markup Language eXtensible Markup Language A subset of SGML (Standard Generalized Markup Language) A subset of SGML (Standard Generalized.
1 Introduction JAXP. Objectives  XML Parser  Parsing and Parsers  JAXP interfaces  Workshops 2.
Martin Kruliš by Martin Kruliš (v1.1)1.
7-Mar-16 Simple API XML.  SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files  DOM is a W3C standard  SAX is an.
13-Mar-16 DOM. 2 Difference between SAX and DOM DOM reads the entire XML document into memory and stores it as a tree data structure SAX reads the XML.
D Copyright © 2004, Oracle. All rights reserved. Using Oracle XML Developer’s Kit.
1 Text File Input and Output. Objectives You will be able to Write text files from your Java programs. Read text files in your Java programs. 2.
21-Jun-16 Document Object Model DOM. SAX and DOM SAX and DOM are standards for XML parsers-- program APIs to read and interpret XML files DOM is a W3C.
Lecture Transforming Data: Using Apache Xalan to apply XSLT transformations Marc Dumontier Blueprint Initiative Samuel Lunenfeld Research Institute.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
XML. Contents  Parsing an XML Document  Validating XML Documents.
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Unit 4 Representing Web Data: XML
Intro to XML.
XML in Web Technologies
Data Modeling II XML Schema & JAXB Marc Dumontier May 4, 2004
XML Parsers Overview Types of parsers Using XML parsers SAX DOM
Topics Introduction to File Input and Output
A parser for XML Documents
DOM 24-Feb-19.
XML: eXtensible Markup Language
Topics Introduction to File Input and Output
XML Programming in Java
XML and Web Services (II/2546)
Presentation transcript:

Java + XML = JDOM by Jason Hunter and Brett McLaughlin co-creators of JDOM Mountain View Java User's Group April 26, 2000

Introductions Jason Hunter K&A Software Author of "Java Servlet Programming" (O'Reilly)

Introductions Brett McLaughlin Metro Information Services Author of upcoming "Java and XML" (O'Reilly)

What is JDOM? JDOM is the Java Document Object ModelJDOM is the Java Document Object Model A way to represent an XML document for easy and efficient reading, manipulation, and writingA 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 build on DOM or modeled after DOMDespite the name similarity, it's not build on DOM or modeled after DOM –Although it integrates well with DOM and SAX –Name chosen for accuracy, not similarity to DOM An open source project with an Apache-style licenseAn open source project with an Apache-style license

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 content, not a child Text node, which has content (ala DOM) –Exceptions should contain useful error messages –Give line numbers and specifics, use no SAX or DOM classes or constructs

More JDOM Philosophy JDOM should integrate with DOM and SAXJDOM should integrate with DOM and SAX –Support reading and writing DOM documents and SAX events –Support runtime plug-in of any DOM or SAX parser –Easy conversion from DOM/SAX to JDOM –Easy conversion from JDOM to DOM/SAX JDOM should stay current with the latest XML standardsJDOM should stay current with the latest XML standards –DOM Level 2, SAX 2.0, XML Schema JDOM does not need to solve every problemJDOM does not need to solve every problem –It should solve 80% of the problems with 20% of the effort –We think we got the ratios to 90% / 10%

The Historical Alternatives: DOM DOM is a large API designed for complex environmentsDOM is a large API designed for complex environments –Represents a document tree fully held in memory –Has to 100% accurately represent any XML document (well, it attempts to) –Has to have the same API on multiple languages –Reading and changing the document is non- intuitive –Fairly heavyweight to load and store in memory

The Historical Alternatives: SAX SAX is a lightweight API designed for fast readingSAX is a lightweight API designed for fast reading –Callback mechanism reports when document elements are encountered –Lightweight since the document is never entirely in memory –Does not support modifying the document –Does not support random access to the document –Fairly steep learning curve to use correctly

Do you need JDOM? JDOM is a lightweight APIJDOM is a lightweight API –Benchmarks of "load and print" show performance on par with SAX –Manipulation and output are also lightning fast JDOM can represent a full documentJDOM can represent a full document –Not all must be in memory at once JDOM supports document modificationJDOM supports document modification –And document creation from scratch, no "factory" JDOM is easy to learnJDOM is easy to learn –Optimized for Java programmers –Doesn't require in-depth XML knowledge –Allows easing into SAX and DOM, if needed –Simple support for namespaces, validation

The Document class 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, and Comment s It can be constructed from scratch:It 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: Document doc = new Document(new Element("rootElement")); Builder builder = new SAXBuilder(); Document doc = builder.build(url);

The Build Process A Document can be constructed using any build toolA Document can be constructed using any build tool –The SAX build tool uses a SAX parser to create a JDOM document Current builders are SAXBuilder and DOMBuilderCurrent builders are SAXBuilder and DOMBuilder –org.jdom.input.SAXBuilder is fast and recommended –org.jdom.input.DOMBuilder is useful for reading an existing DOM tree –A builder can be written that lazily constructs the Document as needed –Other possible builders: LDAPBuilder, SQLBuilder

Builder Classes Builders have optional parameters to specify implementation classes and whether DTD-based validation should occur.Builders have optional parameters to specify implementation classes and whether DTD-based validation should occur. Not all DOM parsers have the same APINot all DOM parsers have the same API –Xerces, XML4J, Project X, Oracle (V1 and V2) –The DOMBuilder adapterClass implements org.jdom.adapters.DOMAdapter –Implements standard methods by passing through to an underlying parser –Adapters for all popular parsers are provided –Future parsers require just a small adapter class Once built, documents are not tied to their build toolOnce built, documents are not tied to their build tool SAXBuilder(String parserClass, boolean validate); DOMBuilder(String adapterClass, boolean validate);

The Output Process A Document can be written using any output toolA Document can be written using any output tool –org.jdom.output.XMLOutputter tool writes the document as XML –org.jdom.output.SAXOutputter tool generates SAX events –org.jdom.output.DOMOutputter tool creates a DOM document (coming soon) –Any custom output tool can be used To output a Document as XML:To output a Document as XML: For machine-consumption, pass optional parametersFor machine-consumption, pass optional parameters –Zero-space indent, no new lines XMLOutputter outputter = new XMLOutputter(); outputter.output(doc, System.out); outputter = new XMLOutputter("", false); outputter.output(doc, System.out);

Pretty Printer import java.io.*; import org.jdom.*; import org.jdom.input.*; import org.jdom.output.*; public class PrettyPrinter { public static void main(String[] args) { // Assume filename argument String filename = args[0]; try { // Build w/ SAX and Xerces, no validation Builder b = new SAXBuilder(); // Create the document Document doc = b.build(new File(filename)); // Output as XML to screen XMLOutputter outputter = new XMLOutputter(); outputter.output(doc, System.out); } catch (Exception e) { e.printStackTrace(); }

The DocType class A Document may have a DocTypeA Document may have a DocType This specifies the DTD of the documentThis specifies the DTD of the document –It's easy to read and write <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " DocType docType = doc.getDocType(); System.out.println("Element: " + docType.getElementName()); System.out.println("Public ID: " + docType.getPublicID()); System.out.println("System ID: " + docType.getSystemID()); doc.setDocType( new DocType("html", "-//W3C...", "

The Element class A Document has a root Element :A Document has a root Element : Get the root as an Element object:Get the root as an Element object: An Element represents something like web-appAn Element represents something like web-app –Has access to everything from the open to the closing –Has access to everything from the open to the closing Gotta fit servlets in somewhere! Element webapp = doc.getRootElement();

Playing with Children An element may contain child elementsAn element may contain child elements getChild() may throw NoSuchElementExceptiongetChild() may throw NoSuchElementException // Get a List of direct children as Elements List allChildren = element.getChildren(); out.println("First kid: " + allChildren.get(0).getName()); // Get all direct children with a given name List namedChildren = element.getChildren("name"); // Get the first kid with a given name Element kid = element.getChild("name"); // Namespaces are supported kid = element.getChild("nsprefix:name"); kid = element.getChild("nsprefix", "name");

Playing with Grandchildren Grandkids can be retrieved easily:Grandkids can be retrieved easily: Future JDOM versions are likely to support XPathFuture JDOM versions are likely to support XPath Enlightenment String manager = root.getChild("gui").getChild("window-manager").getChild("name").getContent();

Managing the Population Children can be added and removed through List manipulation or convenience methods:Children can be added and removed through List manipulation or convenience methods: List allChildren = element.getChildren(); // Remove the fourth child allChildren.remove(3); // Remove all children named "jack" allChildren.removeAll( element.getChildren("jack")); element.removeChildren("jack"); // Add a new child allChildren.add(new Element("jane")); element.addChild(new Element("jane")); // Add a new child in the second position allChildren.add(1, new Element("second"));

Making Kids Elements are constructed directly, no factory method neededElements are constructed directly, no factory method needed Some prefer a nesting shortcut, possible since addChild() returns the Element on which the child was added:Some prefer a nesting shortcut, possible since addChild() returns the Element on which the child was added: A subclass of Element can be made, already containing child elements and contentA subclass of Element can be made, already containing child elements and content Element element = new Element("kid"); Document doc = new Document( new Element("family").addChild(new Element("mom")).addChild(new Element("dad").addChild("kidOfDad"))); root.addChild(new FooterElement());

Making the linux-config Document This code constructs the seen previously:This code constructs the seen previously: Document doc = new Document( new Element("linux-config").addChild(new Element("gui").addChild(new Element("window-manager").addChild(new Element("name").setContent("Enlightenment")).addChild(new Element("version").setContent("0.16.2")) ) );

Getting Element Attributes Elements often contain attributes:Elements often contain attributes: Attributes can be retrieved several ways:Attributes can be retrieved several ways: getAttribute() may throw NoSuchAttributeExceptiongetAttribute() may throw NoSuchAttributeException String value = table.getAttribute("width").getValue(); // Get "border" as an int, default of 2 int value = table.getAttribute("border").getIntValue(2); // Get "border" as an int, no default try { value = table.getAttribute("border").getIntValue(); } catch (DataConversionException e) { }

Setting Element Attributes Element attributes can easily be added or removedElement attributes can easily be added or removed // Add an attribute table.addAttribute("vspace", "0"); // Add an attribute more formally table.addAttribute( new Attribute("prefix", "name", "value")); // Remove an attribute table.removeAttribute("border"); // Remove all attributes table.getAttributes().clear();

Element Content Elements can contain text content:Elements can contain text content: The content is directly available:The content is directly available: And can easily be changed:And can easily be changed: A cool demo String content = element.getContent(); // This blows away all current content element.setContent("A new description");

Mixed Content Sometimes an element may contain comments, text content, and childrenSometimes an element may contain comments, text content, and children Text and children can be retrieved as always:Text and children can be retrieved as always: This keeps the standard uses simpleThis keeps the standard uses simple Some text Some child String text = table.getContent(); Element tr = table.getChild("tr");

Reading Mixed Content To get all content within an Element, use getMixedContent()To get all content within an Element, use getMixedContent() –Returns a List containing Comment, String, and Element objects List mixedContent = table.getMixedContent(); Iterator i = mixedContent.iterator(); while (i.hasNext()) { Object o = i.next(); if (o instanceof Comment) { // Comment has a toString() out.println("Comment: " + o); } else if (o instanceof String) { out.println("String: " + o); } else if (o instanceof Element) { out.println("Element: " + ((Element)o).getName()); }

The ProcessingInstruction class Some documents have ProcessingInstruction sSome documents have ProcessingInstruction s PIs can be retrieved by name and their "attribute" values are directly available:PIs can be retrieved by name and their "attribute" values are directly available: All PIs can be retrieved as a List with doc.getProcessingInstructions()All PIs can be retrieved as a List with doc.getProcessingInstructions() –For simplicity JDOM respects PI order but not the actual placement getProcessingInstruction() may throw NoSuchProcessingInstructionExceptiongetProcessingInstruction() may throw NoSuchProcessingInstructionException ProcessingInstruction cp = doc.getProcessingInstruction( "cocoon-process"); cp.getValue("type");

Namespaces Namespaces are a DOM Level 2 additionNamespaces are a DOM Level 2 addition –JDOM always supports even with DOM Level 1 parsers and even with validation on! Namespace prefix to URI mappings are held in the Document objectNamespace prefix to URI mappings are held in the Document object –Element knows prefix and local name –Document knows prefix to URI mapping –Lets Elements easily move between Documents Retrieve and set a namespace URI for a prefix with:Retrieve and set a namespace URI for a prefix with: This mapping applies even for elements added previouslyThis mapping applies even for elements added previously String uri = doc.getNamespaceURI("linux"); doc.addNamespaceMapping( "linux", "

Using Namespaces Elements have "full names" with a prefix and local nameElements have "full names" with a prefix and local name –Can be specified as two strings –Can be specified as one " prefix:localname " string Allows apps to ignore namespaces if they want.Allows apps to ignore namespaces if they want. Element constructors work the same way.Element constructors work the same way. kid = elt.getChild("JavaXML", "Contents"); kid = elt.getChild("JavaXML:Contents"); kid = elt.getChild("Contents");

List Details The current implementation uses LinkedList for speedThe current implementation uses LinkedList for speed –Speeds growing the List, modifying the List –Slows the relatively rare index-based access All List objects are mutableAll List objects are mutable –Modifications affect the backing document –Other existing list views do not see the change –Same as SQL ResultSet s, etc.

Exceptions JDOMException is the root exceptionJDOMException is the root exception –Thrown for build errors –Always includes a useful error message –May include a "root cause" exception Subclasses include:Subclasses include: –NoSuchAttributeException –NoSuchElementException –NoSuchProcessingInstructionException –DataConversionException

Future There may be a new high-speed builderThere may be a new high-speed builder –Builds a skeleton but defers full analysis –Use of the List interface allows great flexibility There could be other implementations outside org.jdomThere could be other implementations outside org.jdom –The should follow the specification –The current implementation is flexible –We don't expect alternate implementations to be necessary

Get Involved Download the softwareDownload the software – Read the specificationRead the specification –Coming soon Sign up for the mailing lists (see jdom.org )Sign up for the mailing lists (see jdom.org ) –jdom-announce –jdom-interest Watch for JavaWorld and IBM developerWorks articlesWatch for JavaWorld and IBM developerWorks articles – – Help improve the software!Help improve the software!