Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

XML programming in Java technology, Part 1

Picture of the author, drawn by his daughter

(This biography was written by Lily Castle Tidwell, the author's daughter.)

My daddy's name is Doug Tidwell. He has black hair. Daddy wears glasses. He is six feet tall. His eyes are brown.

Daddy likes to play catch with me. He likes to run. He's really fast. He is a really good bike rider. His bike is blue.

My daddy's job is to travel. He goes to China a lot. He has hiked the Great Wall of China. He got a certificate.

I LOVE YOU DADDY!

P.S. Every day that has a d in it, you are the goofiest.

You can contact the author, Doug Tidwell, at dtidwell@us.ibm.com.

Summary:  This updated tutorial covers the basics of manipulating XML documents using Java technology. Doug Tidwell looks at the common APIs for XML and discusses how to parse, create, manipulate, and transform XML documents.

Date:  13 Jan 2004
Level:  Introductory PDF:  A4 and Letter (145 KB | 34 pages)Get Adobe® Reader®

Activity:  30683 views
Comments:  

Introduction

About this tutorial

Over the last few years, XML has become a universal data format. In this updated tutorial, I'll show you the most common programming interfaces for working with XML documents in the Java language.

The most common XML processing task is parsing an XML document. Parsing involves reading an XML document to determine its structure and contents. One of the pleasures of XML programming is the availability of open-source, no-cost XML parsers that read XML documents for you. This tutorial focuses on creating parser objects, asking those parsers to process XML files, and handling the results. As you might expect, you can do these common tasks in several different ways; I'll examine the standards involved as well as when you should use one approach or another.

Programming interfaces

A number of programming interfaces have been created to simplify writing Java programs that process XML. These interfaces have been defined by companies, by standards bodies, and by user groups to meet the needs of XML programmers. In this tutorial, I'll cover the following interfaces:

  • The Document Object Model (DOM), Level 2
  • The Simple API for XML (SAX), Version 2.0
  • JDOM, a simple Java API created by Jason Hunter and Brett McLaughlin
  • The Java API for XML Processing (JAXP)

The first three of these four interfaces (DOM, SAX, and JDOM) define how the contents of an XML document are accessed and represented. JAXP contains classes for creating parser objects. To create DOM or SAX parsers, you'll use JAXP. When you use JDOM, the JDOM library uses JAXP under the covers to create a parser. To sum it all up:

  • You use DOM, SAX, or JDOM to work with the contents of an XML document.
  • If you use DOM or SAX, you use JAXP to create a parser.
  • If you use JDOM, the JDOM library creates a parser for you.

I'll explore the design goals, strengths, and weaknesses of each of these APIs, along with a bit of their histories and the standards bodies that created them.


About the examples

Throughout this tutorial, I'll show you a number of sample programs that use the DOM, SAX, and JDOM APIs. All of them work with an XML-tagged Shakespearean sonnet. The structure of the sonnet is:

 <sonnet>
  <author>
    <lastName>
    <firstName>
    <nationality>
    <yearOfBirth>
    <yearOfDeath>
  </author>
  <lines>
    [14 <line> elements]
  </lines>
</sonnet>
          

For the complete example, see sonnet.xml and sonnet.dtd (download to view in a text editor).


Setting up your machine

You'll need to set up a few things on your machine before you can run the examples. (I'm assuming that you know how to compile and run a Java program, and that you know how to set your CLASSPATH variable.)

  1. First, visit the home page of the Xerces XML parser at the Apache XML Project (http://xml.apache.org/xerces2-j/). You can also go directly to the download page (http://xml.apache.org/xerces2-j/download.cgi).
  2. Unzip the file that you downloaded from Apache. This creates a directory named xerces-2_5_0 or something similar, depending on the release level of the parser. The JAR files you need ( xercesImpl.jar and xml-apis.jar ) should be in the Xerces root directory.
  3. Visit the JDOM project's Web site and download the latest version of JDOM (http://jdom.org/).
  4. Unzip the file you unloaded from JDOM. This creates a directory named jdom-b9 or something similar. The JAR file you need ( jdom.jar ) should be in the build directory.
  5. Finally, download the zip file of examples for this tutorial, xmlprogj.zip, and unzip the file.
  6. Add the current directory (. ), xercesImpl.jar, xml-apis.jar, and jdom.jar to your CLASSPATH.

1 of 9 | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=XML
ArticleID=138359
TutorialTitle=XML programming in Java technology, Part 1
publish-date=01132004
author1-email=dtidwell@us.ibm.com
author1-email-cc=