March 1, 2021
Hot Topics:

JDOM: XML Meets Java Meets Open Source

  • By Piroz Mohseni
  • Send Email »
  • More Articles »

We then need to loop through the item files and figure out which ones are in-stock and include those in the final order as well. We do that using a for loop. Using the

method, we can extract the value for the attribute "instock." For items that are in-stock, we go through the exercise of adding the appropriate information to the "order" document using the
method. This time, we do the extraction and addition in one line. Once we are done, we output the final "order" document which contains elements from various "item" files and the customer.xml file.

If we had to examine a document that had more than one element with the same name, we could use the

method which returns a List containing all the matching elements. Using Java Collection API, we can iterate through the list of elements. Once again, the goal is to use familiar Java constructs (like a List) to simplify XML manipulations.

Creating XML

The objects representing the various parts of an XML document can be constructed independently of each other. These objects can then be tied together to form a hierarchy (nesting relationships). The important thing is that you have control and flexibility over how the various pieces are tied together allowing you to create XML documents from scratch while conforming to various DTDs. We demonstrate this using a servlet. The goals is to collect some information from an HTML form and then produce an XML document based on that information. Listing 4 shows the HTML form:

Listing 4.

Please provide the following information:


Favorite Food:

Favorite Sport:

Indoor Outdoor

Listing 5 shows the servlet that handles the information provided by the above form:

Listing 5.

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class createXML extends HttpServlet {

    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
        PrintWriter out = response.getWriter();

        try {
            Element survey = new Element("survey");
            Element name = new Element("name");
            Element food = new Element("food");
            Element sport = new Element("sport");
            Attribute type = new Attribute("type", 
            Document surveydoc = new Document(survey);
            XMLOutputter xout = new XMLOutputter(" ", true);
            xout.output(surveydoc, out);
       } catch (Exception e) {
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
        doGet(request, response);

For each field, we create an element and then use the

method to insert the text information inside the element. We also create an Attribute and associate it with the "sport" element. Each element is created independently from others. We insert all of them into the root element (survey) using the
method and use the constructor for Document to create the actual XML document object. The creation process is very straightforward. Using the other classes in the org.jdom package, we can independently create comments, CDATA sections, processing instructions, etc. and add them to the XML document.

We have tried to show some of the capabilities of JDOM through some simple examples. Hopefully it is clear to you that JDOM takes an intuitive approach to modeling an XML document into a series of Java objects. These objects are then used like any other Java object to manipulate and modify the XML document. Although JDOM does not have the language independence quality attributed to DOM and SAX, it does provide Java programmers with a simple way to write code for reading, writing and manipulating XML documents. Furthermore, by providing its output in XML, DOM tree and SAX event streams, JDOM does provide a bridge in cases where using a specific standard-based API is necessary. JDOM is evolving and future releases could include support for XSLT and XPath.

About the Author

Piroz Mohseni is president of Bita Technologies, which focuses on business improvement through effective usage of technology. His areas of interest include enterprise Java, XML, and e-commerce applications. Contact him at mohseni@bita-tech.com.

Page 2 of 2

This article was originally published on January 3, 2001

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date