jdom
TRANSCRIPT
![Page 1: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/1.jpg)
04/12/23 JDOM: Part I 1
Introduction to JDOM
Ethan Cerami
New York University
![Page 2: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/2.jpg)
04/12/23 JDOM: Part I 2
Road Map JDOM Overview JDOM v. DOM API Overview Basic Examples XML Validation Traversing XML Trees
![Page 3: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/3.jpg)
04/12/23 JDOM: Part I 3
JDOM Overview 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.
![Page 4: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/4.jpg)
04/12/23 JDOM: Part I 4
JDOM Philosophy From the JDOM Web site:
“Build a better mousetrap and the world will beat a path to your door.”
-- Emerson
![Page 5: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/5.jpg)
04/12/23 JDOM: Part I 5
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
![Page 6: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/6.jpg)
04/12/23 JDOM: Part I 6
JDOM Factoids JDOM Web Site: http://www.jdom.org Official JDOM FAQ:
http://www.jdom.org/docs/faq.html Current Official Release: JDOM Beta 10
![Page 7: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/7.jpg)
04/12/23 JDOM: Part I 7
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.
![Page 8: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/8.jpg)
04/12/23 JDOM: Part I 8
JSR 102 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.
![Page 9: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/9.jpg)
04/12/23 JDOM: Part I 9
JDOM v. DOM
![Page 10: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/10.jpg)
04/12/23 JDOM: Part I 10
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
![Page 11: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/11.jpg)
04/12/23 JDOM: Part I 11
JDOM v. DOM 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.
![Page 12: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/12.jpg)
04/12/23 JDOM: Part I 12
JDOM API Overview
![Page 13: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/13.jpg)
04/12/23 JDOM: Part I 13
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.)
![Page 14: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/14.jpg)
04/12/23 JDOM: Part I 14
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.
![Page 15: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/15.jpg)
04/12/23 JDOM: Part I 15
JDOM Default Parser 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.
![Page 16: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/16.jpg)
04/12/23 JDOM: Part I 16
Basic Example
![Page 17: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/17.jpg)
04/12/23 JDOM: Part I 17
Basic Example jdom1.java: works with local files
Takes an XML file name from the command line.
Displays the specified XML file.
![Page 18: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/18.jpg)
04/12/23 JDOM: Part I 18
XML Input: org.jdom.input 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.)
![Page 19: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/19.jpg)
04/12/23 JDOM: Part I 19
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.
![Page 20: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/20.jpg)
04/12/23 JDOM: Part I 20
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.
![Page 21: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/21.jpg)
04/12/23 JDOM: Part I 21
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.
![Page 22: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/22.jpg)
04/12/23 JDOM: Part I 22
JDOM1 Example To use JDOM1, specify an XML file on the command line. For example:
java jdom1 document.xml Program output:JDOM1 ExampleDownloading 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>St.Laurent</LASTNAME><COMPANY>XML Mania</COMPANY></AUTHOR><SUMMARY>This is element text and an entity follows:This is a
very simple sample document.</SUMMARY></DOCUMENT>
![Page 23: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/23.jpg)
04/12/23 JDOM: Part I 23
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));
![Page 24: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/24.jpg)
04/12/23 JDOM: Part I 24
// Use XMLOutputter XMLOutputter out = new XMLOutputter (); out.output (doc, System.out); } catch (Exception e) { e.printStackTrace(); } }
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]); }
}
![Page 25: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/25.jpg)
04/12/23 JDOM: Part I 25
XML Validation
![Page 26: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/26.jpg)
04/12/23 JDOM: Part I 26
JDOMException 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…
![Page 27: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/27.jpg)
04/12/23 JDOM: Part I 27
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE DOCUMENT SYSTEM "simple.dtd"><DOCUMENT trackNum="1234"><TITLE>Sample Document</TITLE><AUTHOR><FIRSTNAME>Simon</FIRSTNAME><LASTNAME>St.Laurent</LASTNAME><COMPANY>XML Mania</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.
![Page 28: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/28.jpg)
04/12/23 JDOM: Part I 28
Well-formedness Errors jdom1 will output the following error:
JDOM1 ExampleDownloading file: document2.xmlorg.jdom.JDOMException: Error on line 10 of document
file:/home1/e/eqc3844/xml/document2.xml: The element type "DOCUMENT" must be terminated by the matching end -tag "</DOCUMENT>".
at org.jdom.input.SAXBuilder.build(SAXBuilder.java:296) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:617) at org.jdom.input.SAXBuilder.build(SAXBuilder.java:599) at jdom1.process(jdom1.java:20) at jdom1.main(jdom1.java:34)
![Page 29: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/29.jpg)
04/12/23 JDOM: Part I 29
Validation 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.
![Page 30: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/30.jpg)
04/12/23 JDOM: Part I 30
JDOM2 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-formedDocument is valid
![Page 31: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/31.jpg)
04/12/23 JDOM: Part I 31
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) {
![Page 32: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/32.jpg)
04/12/23 JDOM: Part I 32
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 well-formed"); 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]); }}
![Page 33: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/33.jpg)
04/12/23 JDOM: Part I 33
JDOM2 in Action Let’s try JDOM2 on the XML document
on the next slide…
![Page 34: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/34.jpg)
04/12/23 JDOM: Part I 34
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE DOCUMENT SYSTEM "simple.dtd"><DOCUMENT trackNum="1234"><TITLE>Sample Document</TITLE><AUTHOR><FIRSTNAME>Simon</FIRSTNAME><COMPANY>XML Mania</COMPANY></AUTHOR><SUMMARY>This is element text and an entity follows:This is a very simple sample document.</SUMMARY></DOCUMENT>
The <LASTNAME> element is missing.Hence, document is invalid.
![Page 35: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/35.jpg)
04/12/23 JDOM: Part I 35
JDOM2 Output JDOM2 will report the validity error:
JDOM2 Example with Validation
Downloading file: document3.xml
JDOM Exception: Error on line 6 of document file:/home1/e/eqc3844/xml/document3.xml: The content of element type "AUTHOR" must match "(FIRSTNAME,LASTNAME,(UNIVERSITY|COMPANY)?)".
![Page 36: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/36.jpg)
04/12/23 JDOM: Part I 36
Traversing XML Trees
![Page 37: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/37.jpg)
04/12/23 JDOM: Part I 37
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.
![Page 38: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/38.jpg)
04/12/23 JDOM: Part I 38
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.
![Page 39: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/39.jpg)
04/12/23 JDOM: Part I 39
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.
![Page 40: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/40.jpg)
04/12/23 JDOM: Part I 40
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.
![Page 41: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/41.jpg)
04/12/23 JDOM: Part I 41
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.
![Page 42: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/42.jpg)
04/12/23 JDOM: Part I 42
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.
![Page 43: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/43.jpg)
04/12/23 JDOM: Part I 43
Sample Source File<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE moreovernews SYSTEM
"http://p.moreover.com/xml_dtds/moreovernews.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/</document_url> <harvest_time>Dec 1 2001 10:59AM</harvest_time> <access_registration> </access_registration> <access_status> </access_status> </article>
![Page 44: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/44.jpg)
04/12/23 JDOM: Part I 44
Sample Outputmoreovernewsarticleurlheadline_textsourcemedia_typeclustertaglinedocument_urlharvest_timeaccess_registrationaccess_statusarticleurl…
The programsimply outputs all elementswithin the XMLdocument.
![Page 45: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/45.jpg)
04/12/23 JDOM: Part I 45
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 {
![Page 46: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/46.jpg)
04/12/23 JDOM: Part I 46
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()); } }
![Page 47: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/47.jpg)
04/12/23 JDOM: Part I 47
// 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); } }
![Page 48: jdom](https://reader036.vdocuments.site/reader036/viewer/2022081508/5571f20d49795947648c0cf1/html5/thumbnails/48.jpg)
04/12/23 JDOM: Part I 48
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"); }
}