JDOM Overview. Application development with XML and Java. Application Development with XML and Java. JDOM Philosophy. JDOM and Sun



Similar documents
XML & Databases. Tutorial. 2. Parsing XML. Universität Konstanz. Database & Information Systems Group Prof. Marc H. Scholl

by LindaMay Patterson PartnerWorld for Developers, AS/400 January 2000

Last Week. XML (extensible Markup Language) HTML Deficiencies. XML Advantages. Syntax of XML DHTML. Applets. Modifying DOM Event bubbling

N CYCLES software solutions. XML White Paper. Where XML Fits in Enterprise Applications. May 2001

API Document Webaroo Technology India Pvt. Ltd.

Java Application Developer Certificate Program Competencies

Oracle WebLogic Server

Java CPD (I) Frans Coenen Department of Computer Science

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Introduction to XML Applications

High Performance XML Data Retrieval

Division of Informatics, University of Edinburgh

Overview of Web Services API

Using the Monitoring and Report Viewer Web Services

CST6445: Web Services Development with Java and XML Lesson 1 Introduction To Web Services Skilltop Technology Limited. All rights reserved.

Processing XML with Java A Performance Benchmark

Cisco BTS Softswitch SOAP Adapter Interface Specification Programmer s Guide, Release 7.0

Chapter 1 Java Program Design and Development

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Services. Custom Tag Libraries. Today. Web Development. Role-Based. Development. Code Reuse. Tag Libraries Custom Tags. Tag Lifecycle.

JBoss Portlet Container. User Guide. Release 2.0

Java Programming Language

<Insert Picture Here> What's New in NetBeans IDE 7.2

Managing large sound databases using Mpeg7

Amazon Glacier. Developer Guide API Version

Developing XML Solutions with JavaServer Pages Technology

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

DTS Web Developers Guide

Logging in Java Applications

Selenium Automation set up with TestNG and Eclipse- A Beginners Guide

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

What Perl Programmers Should Know About Java

Connecting Custom Services to the YAWL Engine. Beta 7 Release

Install Java Development Kit (JDK) 1.8

OpenOffice.org Extensions development in Java with NetBeans in practise. Jürgen Schmidt OpenOffice.org Sun Microsystems, Inc.

Contents. 9-1 Copyright (c) N. Afshartous

JobScheduler Web Services Executing JobScheduler commands

Overview of DatadiagramML

Lecture J - Exceptions

Java SE 8 Programming

Web Service Caching Using Command Cache

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

DTD Tutorial. About the tutorial. Tutorial

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Specialized Programme on Web Application Development using Open Source Tools

Pre-authentication XXE vulnerability in the Services Drupal module

Specialized Programme on Web Application Development using Open Source Tools

Session Topic. Session Objectives. Extreme Java G XML Data Processing for Java MOM and POP Applications

Evaluation. Copy. Evaluation Copy. Chapter 7: Using JDBC with Spring. 1) A Simpler Approach ) The JdbcTemplate. Class...

Remote Method Invocation in JAVA

CHAPTER 6 EXTRACTION OF METHOD SIGNATURES FROM UML CLASS DIAGRAM

5 HDFS - Hadoop Distributed System

Fundamentals of Java Programming

REDUCING THE COST OF GROUND SYSTEM DEVELOPMENT AND MISSION OPERATIONS USING AUTOMATED XML TECHNOLOGIES. Jesse Wright Jet Propulsion Laboratory,

KC Data Integration Web Service Developer Guide

Specific Simple Network Management Tools

Hibernate Language Binding Guide For The Connection Cloud Using Java Persistence API (JAP)

DataDirect XQuery Technical Overview

How To Create A Native Ad On A Nast On A Pc Or Mac Or Ipad (For Android) On A Mac Or Mac) On Pc Or Ipa (For Mac Or Pc) On An Android Or Ipam (For Pc Or

Using Files as Input/Output in Java 5.0 Applications

Advanced Object Oriented Database access using PDO. Marcus Börger

XML Processing and Web Services. Chapter 17

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Layer2 Term Set Glossary App for SharePoint 2013

Building a Multi-Threaded Web Server

Efficiency of Web Based SAX XML Distributed Processing

Using Eclipse CDT/PTP for Static Analysis

Copy the.jar file into the plugins/ subfolder of your Eclipse installation. (e.g., C:\Program Files\Eclipse\plugins)

How to Design and Create Your Own Custom Ext Rep

JMS Messages C HAPTER 3. Message Definition

02 B The Java Virtual Machine

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

An Overview of Java. overview-1

An XML Based Data Exchange Model for Power System Studies

LabVIEW Internet Toolkit User Guide

This material is built based on, Patterns covered in this class FILTERING PATTERNS. Filtering pattern

MarkLogic Server. Java Application Developer s Guide. MarkLogic 8 February, Copyright 2015 MarkLogic Corporation. All rights reserved.

DEVELOPING CONTRACT - DRIVEN WEB SERVICES USING JDEVELOPER. The purpose of this tutorial is to develop a java web service using a top-down approach.

Tutorial: setting up a web application

ECE 122. Engineering Problem Solving with Java

Appendix A Using the Java Compiler

Java EE Web Development Course Program

Managing XML Documents Versions and Upgrades with XSLT

Beyond The Web Drupal Meets The Desktop (And Mobile) Justin Miller Code Sorcery Workshop, LLC

Database Access from a Programming Language: Database Access from a Programming Language

Database Access from a Programming Language:

Introduction to HP ArcSight ESM Web Services APIs

Java 7 Recipes. Freddy Guime. vk» (,\['«** g!p#« Carl Dea. Josh Juneau. John O'Conner

Java Interview Questions and Answers

Lesson: All About Sockets

IBM Rational Rapid Developer Components & Web Services

AP Computer Science Java Subset

Transcription:

JDOM Overview Application Development with XML and Java Lecture 7 XML Parsing JDOM JDOM: Java Package for easily reading and building XML documents. Created by two programmers: Brett McLaughlin and Jason Hunter. Started as an open source project in 2000. Started because of programmers general frustration with the regular DOM API Package. From the JDOM Web site: Build a better mousetrap and the world will beat a path to your door. -- Emerson JDOM Web Site: http://www.jdom.org Official JDOM FAQ: http://www.jdom.org/docs/faq.html JDOM Philosophy JDOM should be straightforward for Java programmers. JDOM should support easy and efficient document modification. JDOM should hide the complexities of XML wherever possible, while remaining true to the XML specification. JDOM should integrate with DOM and SAX. JDOM should be lightweight and fast. JDOM should solve 80% (or more) of Java/XML problems with 20% (or less) of the effort JDOM and Sun Sun maintains a process called, Java Community Process This enables groups of developers to suggest new API for the core Java platform. To be considered, a new API is given a Java Specification Request (JSR.) JSRs are then reviewed, and are either accepted into the Java Platform or rejected. JDOM has been under consideration as JSR 102. Recently, JSR 102 was approved for eventual inclusion into the core Java platform. It is currently unclear which package JDOM will be included in: the standard kit or the enterprise kit. Either way, JDOM will probably become an official part of Java. JDOM is fast becoming a standard, and has received widespread support.

JDOM v. DOM JDOM v. DOM DOM: The main issue with the DOM is that it was defined to be language independent. This is generally a good thing, because you can therefore use the DOM in multiple languages, e.g. C, C++, Perl, Java, etc. However, it also presents several problems, including: DOM is not optimized for Java, and DOM API is very large JDOM JDOM was designed for Java optimization. Makes it much easier for regular Java programmers. JDOM API is much smaller and more manageable than the DOM API. JDOM JDOM was designed for Java optimization. Makes it much easier for regular Java programmers. JDOM API is much smaller and more manageable than the DOM API. JDOM API Overview JDOM has four main packages: org.jdom: classes that represent an XML document and its parts. org.jdom.input: classes for reading a document into memory. org.jdom.output: classes for writing a document to a stream or file. org.jdom.adapters: classes for hooking up to DOM implementations (we are skipping this package.) JDOM Parsers JDOM is not actually an XML parser. Rather, JDOM is an interface for manipulating/creating XML documents. To work, JDOM needs an XML parser to do the actual parsing. JDOM works with lots of different parsers: Oracle XML Parser Sun XML Parser etc., etc. By default, JDOM will use the Xerces XML Parser. Xerces is automatically bundled with JDOM, so it works right out of the box. All the examples in class assume the default Xerces parser.

Basic Example jdom1.java: works with local files Takes an XML file name from the command line. Displays the specified XML file. To read in an XML file, use the org.jdom.input package. There are two main options: SAXBuilder: uses a SAX parser to build the document (faster option, recommended.) DOMBuilder: uses a DOM parser to build the document (slower option, not recommended.) Using the SAXBuilder To read in a local file via the SAXBuilder: SAXBuilder builder = new SAXBuilder(); Document doc=builder.build(new File(fileName)); Once you have a builder object, call the build() method, and specify a File object. The build() method will return a Document object. The Document object encapsulates all data regarding your XML document. XML Output: org.jdom.output To output an XML document, use the org.jdom.output package. The main class here is the XMLOutputter class. XMLOutputter is used to easily output any XML document to the screen or a specific file. XMLOutputter To output an XML document to the screen: XMLOutputter out = new XMLOutputter (); out.output (doc, System.out); Here, you are specifying an XML Document object, and an output stream.

JDOM1 Example To use JDOM1, specify an XML file on the command line. For example: java jdom1 document.xml Program output: JDOM1 Example Downloading file: document.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE DOCUMENT SYSTEM "simple.dtd"> <DOCUMENT tracknum="1234" seclevel="unclassified"> <TITLE>Sample Document</TITLE> <AUTHOR> <LASTNAME>Kosmopoulos</LASTNAME> <COMPANY>City</COMPANY></AUTHOR> <SUMMARY>This is element text and an entity follows:this is a very simple sample document. </SUMMARY> </DOCUMENT> import avail.*; import org.jdom.*; import org.jdom.input.saxbuilder; import org.jdom.output.xmloutputter; /** * Basic JDOM Example * Outputs any local XML file specified on the command line * Example usage: * java jdom1 document.xml */ public class jdom1 { // Download and Output XML File public void process (String filename) { try { // Use SAXBuilder SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File(fileName)); JDOMException // Use XMLOutputter XMLOutputter out = new XMLOutputter (); out.output (doc, System.out); catch (Exception e) { e.printstacktrace(); In the event of a well-formedness error, JDOM will throw a JDOMException. For example, let us parse the XML document on the next slide public static void main (String[] args) { System.out.println ("JDOM1 Example"); System.out.println ("Downloading file: "+args[0]); jdom1 app = new jdom1(); app.process(args[0]);

Well-formedness Errors <?xml version="1.0" encoding="utf- 8"?> <!DOCTYPE DOCUMENT SYSTEM "simple.dtd"> <DOCUMENT tracknum="1234"> <TITLE>Sample Document</TITLE> <AUTHOR> <FIRSTNAME>Kosmas</FIRSTNAME> <LASTNAME>Kosmopoulos</LASTNAME> <COMPANY>City</COMPANY></AUTHOR> <SUMMARY>This is element text and an entity follows: This is a very simple sample document. </SUMMARY> End </DOCUMENT> tag is missing. Hence, document is not well-formed. jdom1 will output the following error: JDOM1 Example Downloading file: document2.xml org.jdom.jdomexception: Error on line 10 of document file:/home1/e/eqc3844/xml/document 2.xml: The element type "DOCUMENT" must be terminated by the matching end -tag "</DOCUMENT>". at org.jdom.input.saxbuilder.build(sa XBuilder.java:296) at org.jdom.input.saxbuilder.build(sa XBuilder.java:617) at org.jdom.input.saxbuilder.build(sa XBuilder.java:599) at jdom1.process(jdom1.java:20) at jdom1.main(jdom1.java:34) Validation JDOM2 By default, validation is turned off. Hence, by default, errors in validity are completely ignored. To turn validation on, pass true to the Builder constructor. For example: SAXBuilder builder = new SAXBuilder (true); With validation turned on, validation errors are reported as JDOMExceptions. JDOM2 turns validation on. Errors in well-formedness are reported as JDOMExceptions. Errors in validity are also reported as JDOMExceptions. If no errors occur, program outputs: Document is well-formed Document is valid

import avail.*; import org.jdom.*; import org.jdom.input.saxbuilder; import org.jdom.output.xmloutputter; /** * Basic JDOM Example * Outputs any local XML file specified on the command line * Performs XML Validation * Example usage: * java jdom2 document.xml */ public class jdom2 { // Download and Output XML File public void process (String filename) { try { // Use SAXBuilder // turn XML validation on SAXBuilder builder = new SAXBuilder (true); Document doc = builder.build(new File(fileName)); // If we get here without any exceptions, // the document is both well-formed and valid System.out.println ("Document is wellformed"); System.out.println ("Document is valid"); catch (JDOMException e) { System.out.println ("JDOM Exception: "+e.getmessage()); public static void main (String[] args) { System.out.println ("JDOM2 Example with Validation"); System.out.println ("Downloading file: "+args[0]); jdom2 app = new jdom2(); app.process(args[0]); JDOM Tree Every XML document will have a root element. For example, in the following XML document: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE WEATHER SYSTEM "Weather.dtd"> <WEATHER> <CITY NAME="Hong Kong"> <HI>87</HI> <LOW>78</LOW> </CITY> </WEATHER> WEATHER is the root element. Working with the Root Node To get the root Element, use the following code: SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new URL(url)); Element root = doc.getrootelement(); Once you have the root node, you can walk through the XML tree. Element Data Given any XML element, you can retrieve the element name or the embedded text. For example: element.getname(): Returns name of the element. element.gettext(): Returns embedded text.

Getting Element Children You can also query an Element for a list of children. For example, given the following XML: <CITY NAME="Hong Kong"> <HI>87</HI> <LOW>78</LOW> </CITY> The CITY element has two children: HI and LOW. Getting Element Children To get a List of children, call the getchildren() method. Example: List kids = element.getchildren(); Iterator iterator = kids.iterator(); while (iterator.hasnext()) { Element kid = (Element) iterator.next(); processelement (kid); getchildren() returns a Java List object. You can then iterate through the list of children. Example: jdom3.java jdom3.java downloads an RSS News File containing top news stories. Once downloaded, the program walks the entire XML tree, and simply prints out the names of all elements within the document. It also keeps a running count of the number of elements. Sample Source File <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE moreovernews SYSTEM "http://p.moreover.com/xml_dtds/moreo vernews.dtd"> <moreovernews> <article id="_28421464"> <url>http://c.moreover.com/click/here.pl?x28421450</url> <headline_text>niaid Establishes Functional Genomic Research Center</headline_text> <source>unisci</source> <media_type>text</media_type> <cluster>moreover...</cluster> <tagline> </tagline> <document_url>http://unisci.com/</doc ument_url> <harvest_time>dec 1 2001 10:59AM</harvest_time> <access_registration> </access_registration> <access_status> </access_status> </article>

Sample Output moreovernews article url headline_text source media_type cluster tagline document_url harvest_time access_registration access_status article url The program simply outputs all elements within the XML document. import avail.*; import java.net.*; import java.util.list; import java.util.iterator; import org.jdom.*; import org.jdom.input.saxbuilder; import org.jdom.output.xmloutputter; /** * Basic JDOM Example * Illustrates how to traverse a JDOM tree * Example usage: * java jdom4 */ public class jdom4 { private int numelements = 0; // Download and Output XML File public void process (String url) throws MalformedURLException { try { // Use SAXBuilder SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new URL(url)); Element root = doc.getrootelement(); processelement (root); System.out.println ("Total Number of Elements Processed: " +numelements); catch (JDOMException e) { System.out.println ("JDOM Exception: "+e.getmessage()); // Recursive Function to Process Elements // Prints the Element Name and keeps a running count // out total number of elements. private void processelement (Element element) { numelements++; String elementname = element.getname(); System.out.println (elementname); List kids = element.getchildren(); Iterator iterator = kids.iterator(); while (iterator.hasnext()) { Element kid = (Element) iterator.next(); processelement (kid); // Recursive

public static void main (String[] args) throws Exception { System.out.println ("JDOM3 Example"); jdom3 app = new jdom3(); app.process("http://p.moreover.com/cgi -local/page?"+ "c=top%20stories&o=xml");