<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<article id="JDOM">
  <articleinfo>
    <title>JDOM</title>
    <author>
      <firstname>Ashley</firstname>
      <othername>J.S</othername>
      <surname>Mills</surname>
      <affiliation>
        <address><email>ashley@ashleymills.com</email></address>
      </affiliation>
    </author>

    <copyright>
      <year>2005</year>
      <holder role="mailto:ashley@ashleymills.com">The University Of Birmingham</holder>
    </copyright>
  </articleinfo>

  <sect1 id="JDOM-Introduction"><title>Introduction</title>
    <para>
      JDOM is a Java <acronym>API</acronym> that provides the user with the ability to create and manipulate <acronym>XML</acronym> documents.  JDOM provides Java specific <acronym>XML</acronym> functionality.
    </para>
  </sect1>

  <sect1 id="JDOM-Installation"><title>Installation</title>
    <para>
      It is assumed that you know how to setup environment variables and install software on your operating system, for a comprehensive guide to doing so for Windows see <ulink url="../winenvars/winenvarshome.html"><citetitle>Configuring A Windows Working Environment</citetitle></ulink>, for Unix see <ulink url="../unixenvars/unixenvarshome.html"><citetitle>Configuring A Unix Working Environment</citetitle></ulink> Follow these instructions to install JDOM:
    </para>

    <orderedlist>
      <listitem>
        <para>
          Download and unzip the latest binary distribution of JDOM from <ulink url="http://www.jdom.org/dist/binary/">http://www.jdom.org/dist/binary/</ulink>.
        </para>
      </listitem>
      <listitem>
        <para>
          Add <filename>/path/to/wherever-you-unzipped-jdom/build/jdom.jar</filename> to your <envar>CLASSPATH</envar> environment variable.
        </para>
      </listitem>
    </orderedlist>

    <para>The JDOM <acronym>API</acronym> is now ready to use.  The <acronym>API</acronym> docs are situated at <filename>/path/to/wherever-you-unzipped-jdom/build/apidocs/</filename>, these are the canonical resource for programming with JDOM much as the Java API is the canonical resource for programming with Java in general.</para>
  </sect1>

  <sect1 id="JDOM-Basics"><title>JDOM Basics</title>
    <para>
      Since JDOM is an inherently specialist tool there are not really any <quote>basics</quote> because most people who use JDOM will be doing so for a specific reason.  We will introduce JDOM with two basic examples so that you can get an idea of how to use JDOM. A basic understanding of Java Objects, classes and inheritance is assumed.
    </para>

    <sect2 id="JDOM-Basics-InputExample"><title>Getting <acronym>XML</acronym> Input with JDOM</title>
      <para>
        Getting <acronym>XML</acronym> input with JDOM is extremely simple to do, the following example will illustrate this:
      </para>

      <example id="JDOM-Basics-InputExample-Example"><title>JDump</title>
        <programlisting>
// Illustrates JDOM basic XML input capabilities using SAXBuilder
import org.jdom.Document; <co id="JDOM-Basics-InputExample-Example-Import"/>
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
public class JDump {
   public static void main(String[] args) {
      if(args.length == 0) {<co id="JDOM-Basics-InputExample-Example-Args"/>
         System.out.println(&quot;USAGE: JDump URIofXMLDoc&quot;);
         System.exit(0);
      }

      // create the SAXBuilder
      SAXBuilder builder = new SAXBuilder(); <co id="JDOM-Basics-InputExample-Example-CreateSAXBuilder"/>

      try {
        Document doc = builder.build(args[0]);
        XMLOutputter output = new XMLOutputter(&quot;  &quot;, true);<co id="JDOM-Basics-InputExample-Example-Dump"/>
        output.output(doc, System.out);
      } 
      catch(JDOMException e) {
         System.out.println(e.getMessage());<co id="JDOM-Basics-InputExample-Example-Errors"/>
      }
      catch(IOException e) {
         System.out.println(e.getMessage());   
      }
   }
}
        </programlisting>
      </example>

      <calloutlist>
        <callout arearefs="JDOM-Basics-InputExample-Example-Import">
          <para/>
          <programlisting>
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
          </programlisting>
          <para>
            The JDOM and Java classes required are imported.
          </para>
        </callout>
        <callout arearefs="JDOM-Basics-InputExample-Example-Args">
          <para/>
          <programlisting>
      if(args.length == 0) {
         System.out.println(&quot;USAGE: JDump URIofXMLDoc&quot;);
         System.exit(0);
      }
          </programlisting>
          <para>
            The number of command line arguments is checked, if it is zero, a message is produced summarizing usage.
          </para>
        </callout>
        <callout arearefs="JDOM-Basics-InputExample-Example-CreateSAXBuilder">
          <para/>
          <programlisting>
      SAXBuilder builder = new SAXBuilder();
          </programlisting>
          <para>
            A new builder is created to build a JDOM tree.  In this case, a <emphasis>SAXBuilder</emphasis> has been used, which builds a JDOM tree with SAX: <ulink url="http://www.saxproject.org/">http://www.saxproject.org/</ulink>.  The other builder available is <emphasis>DOMBuilder</emphasis> which builds a JDOM tree using <acronym>DOM</acronym>.  It should preferably be used for building from a pre-existing <acronym>DOM</acronym> tree because otherwise a <acronym>DOM</acronym> tree will be built from the <acronym>XML</acronym> file first which is inefficient, SAX should be used instead.  JDOM will use the default validating parser, there is another constructor available for specifying a validating parser to use. 
          </para>
        </callout>
        <callout arearefs="JDOM-Basics-InputExample-Example-Dump">
          <para/>
          <programlisting>
      try {
        Document doc = builder.build(args[0]);
        XMLOutputter output = new XMLOutputter(&quot;  &quot;, true);
        output.output(doc, System.out);
      } 
          </programlisting>
          <para>
            First of all a new <emphasis>Document</emphasis> is created, which is a JDOM tree representation of an <acronym>XML</acronym> tree. <emphasis role="strong">builder.build(args[0])</emphasis> builds a new <emphasis>Document</emphasis> based on the first command line argument, the method used is:
          </para>

          <programlisting>
build(java.net.URL URL) 
           This builds a document from the supplied URL.
          </programlisting>

          <para>
            The URL supplied can be a filename.  There are many other <emphasis>build</emphasis> methods that build Documents based on other forms of input, the JDOM <acronym>API</acronym> lists them all.
          </para>

          <programlisting>
        XMLOutputter output = new XMLOutputter(&quot;  &quot;, true);
        output.output(doc, System.out);
          </programlisting>

          <para>
            If the document is valid, no exception will be thrown and the desired action is to dump the document to stdout; an <emphasis>XMLOutputter</emphasis> Object is created with two parameters, the first specifies the desired indentation width the nested elements should have when output and the second specifies that newlines should be produced where appropriate. <emphasis>output</emphasis> is then called with two parameters, the first specifying the <emphasis>Document</emphasis> to output and the second specifying where to output the <emphasis>Document</emphasis>, in this case, <emphasis>System.out</emphasis>.
          </para>
        </callout>
        <callout arearefs="JDOM-Basics-InputExample-Example-Errors">
          <para/>
          <programlisting>
      catch(JDOMException e) {
         System.out.println(e.getMessage());
      }
      catch(IOException e) {
         System.out.println(e.getMessage());   
      }
          </programlisting>
          <para>
            Two errors may be thrown by the last <emphasis>try</emphasis> statement, the first is a <emphasis>JDOMException</emphasis>, this is thrown by the <emphasis>SAXBuilder</emphasis> Object if it either fails to parse the document correctly or the document does not exist, in either case the error message generated by the object is printed.  The second is an <emphasis>IOException</emphasis> this is thrown by the <emphasis>XMLOutputter</emphasis> Object if it encounters errors outputting the file, if this happens  the error message generated by the object is printed. 
          </para>
        </callout>
      </calloutlist>

      <para>That's it! It is very simple to do and very intuitive.</para>
    </sect2>

    <sect2 id="JDOM-Basics-OutputExample"><title>Creating and Outputting <acronym>XML</acronym> with JDOM</title>

      <para>
        The following example may look daunting if you are a Java beginner but study it hard and read the comments and you should be able to understand it.  The code generates valid <acronym>XHTML</acronym> files, it tries to center the table using two different <acronym>CSS</acronym> methods, one of which is deprecated. At the moment there is not a single multi-supported way of centering a table using only <acronym>CSS</acronym>, if you know, please tell me. Copy the following code into your favorite text editor, study it a little and try to work out what is going on:
      </para>

      <example id="JDOM-Example-XHML-Maker"><title><acronym>XHTML</acronym> Profile Generator</title>
        <programlisting>
// import the JDOM and io stuff we need<co id="JDOM-Example-XHML-Maker-Import"/>
import org.jdom.*;
import org.jdom.output.XMLOutputter;
import java.io.*;
// generates an xhtml 'profile' document based on user input
public class xhtmlgen {
   private static BufferedReader reader;
   public static void main(String[] args) {
      // setup reader for getting user input
      reader = new BufferedReader(
         new InputStreamReader(System.in)
      );

      // create new XML document, set DocType and set root element
      Document profile = new Document();<co id="JDOM-Example-XHML-Maker-CreateDoc"/>
      DocType type     = new DocType(&quot;html&quot;, &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;, 
                                     &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;);
      profile.setDocType(type);
      Element html     = new Element(&quot;html&quot;);
      profile.setRootElement(html);

      // get the persons name
      System.out.println(&quot;Hi there, welcome to ProfileGen 1.0&quot;);<co id="JDOM-Example-XHML-Maker-GetName"/>
      String name = getUserInput(&quot;What is your name?&quot;); 

      // create head element, title (set by name) and meta (predefined) and styles (predefined)
      Element head     = new Element(&quot;head&quot;);<co id="JDOM-Example-XHML-Maker-Element"/>
      head.addContent  ( new Element(&quot;title&quot;).setText(name+&quot;'s Profile&quot;));
      Element meta     = new Element(&quot;meta&quot;);
      meta.setAttribute( new Attribute(&quot;http-equiv&quot;, &quot;Content-Type&quot;));
      meta.setAttribute( new Attribute(&quot;content&quot;, &quot;text/html; charset=utf-8&quot;));
      head.addContent(meta);
      Element style    = new Element(&quot;style&quot;);
      style.setAttribute( new Attribute(&quot;type&quot;, &quot;text/css&quot;));
      style.setText(&quot;h1 { text-align: center; } &quot; +
                    &quot;div.centered {text-align: center;} &quot; +
                    &quot;div.centered table {margin: 0 auto; text-align: left;}&quot;); 

      head.addContent(style);
      html.addContent(head);

      // create body
      Element body     = new Element(&quot;body&quot;);
      Element h1       = new Element(&quot;h1&quot;);
      h1.setAttribute(   new Attribute(&quot;align&quot;, &quot;center&quot;));
      h1.setText(name+&quot;'s Profile&quot;);
      body.addContent(h1);
      body.addContent(   new Element(&quot;hr&quot;));

      // create table and add profile items to it
      Element table       = new Element(&quot;table&quot;);
      table.addContent(createTableEntry(&quot;Name&quot;, name));<co id="JDOM-Example-XHML-Maker-Table"/>
      table.addContent(createDefinedTableEntry(&quot;Age&quot;,&quot;How old are you?&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Sex&quot;,&quot;What is your sex? (M/F)&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Country Of Origin&quot;,&quot;What is your country of origin?&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Occupation&quot;,&quot;What is your occupation?&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Email&quot;,&quot;What is your email address?&quot;)); 

      // let the user add arbitrary elements to the list
      String identifier=null;
      String description=getUserInput(&quot;Would you like to add extra items? (y/n)&quot;);
      while(!description.equals(&quot;n&quot;)) {
         table.addContent(createCustomTableEntry()); 
         description=getUserInput(&quot;Add another? (y/n)&quot;);
      }

      // add the table element (centered by div) to the body element and add final horizontal line
      Element div = new Element(&quot;div&quot;);
      div.setAttribute(&quot;class&quot;, &quot;centered&quot;); // css mess
      div.addContent(table);
      body.addContent(div);
      body.addContent(new Element(&quot;hr&quot;));
      
      // add the body element to the html (root) element
      html.addContent(body);
      
      // initialize FileWriter, filename created from persons name
      FileWriter writer = null;
      try {
      writer = new FileWriter(name+&quot;_Profile.html&quot;);<co id="JDOM-Example-XHML-Maker-MakeFile"/>
      } catch(Exception e) {}

      // initialize JDOM XMLOutputter with 2 spaces indent and newlines on
      XMLOutputter prettyOutput = new XMLOutputter(&quot;  &quot;, true);<co id="JDOM-Example-XHML-Maker-XMLOut"/>

      // output the document to the file (via writer) and close writer 
      try {
         prettyOutput.output(profile, writer);
         writer.close();
      } catch(Exception e) {}
   }

   // creates a html table entry element which embodies identifier and description
   private static Element createTableEntry(String identifier, String description) {
     Element tr     = new Element(&quot;tr&quot;);
     Element td1    = new Element(&quot;td&quot;);
     Element td2    = new Element(&quot;td&quot;);
     Element strong = new Element(&quot;strong&quot;);<co id="JDOM-Example-XHML-Maker-TableEntry"/>
     strong.setText(identifier+&quot;: &quot;);
     td1.addContent(strong);
     td2.setText(description);
     tr.addContent(td1);
     tr.addContent(td2);
     return tr;
   }

   // creates a html table entry element based on identifier and user input prompted by prompt
   private static Element createDefinedTableEntry(String identifier, String prompt) {
     return createTableEntry(identifier, getUserInput(prompt));
   }

   // creates a custom html table entry element defined by the user
   private static Element createCustomTableEntry() { 
      String identifier = getUserInput(&quot;Please enter the profile identifier e.g Name&quot;);
      String description= getUserInput(&quot;Please enter the answer to the identifier, e.g Peter&quot;);
      return createTableEntry(identifier, description);
   }

   // gets user input response to supplied prompt
   private static String getUserInput(String prompt) {
      String userinput = &quot;&quot;;
      while(userinput.equals(&quot;&quot;)) {
         System.out.println(prompt);
         try { 
            userinput = reader.readLine(); 
         } catch(IOException e) { System.err.println(e); }
      }
      return userinput;
   }
}      
        </programlisting>
      </example>


      <calloutlist>
        <callout arearefs="JDOM-Example-XHML-Maker-Import">
          <para/>
          <programlisting>
// import the JDOM and io stuff we need
import org.jdom.*;
import org.jdom.output.XMLOutputter;
import java.io.*;
          </programlisting>
          <para>The program begins by importing the jdom components that we are going to use.</para>
        </callout>
        <callout arearefs="JDOM-Example-XHML-Maker-CreateDoc">
          <para/>
          <programlisting>
      // create new xml document, set DocType and set root element
      Document profile = new Document();
      DocType type     = new DocType(&quot;html&quot;, &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;, 
                                     &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;);
      profile.setDocType(type);
      Element html     = new Element(&quot;html&quot;);
      profile.setRootElement(html);
          </programlisting>
          <para>
            Here we see how we can create the <acronym>XML</acronym> document, it is constructed using the Jdom document class.  The no parameter constructor has been used here so we can illustrate more features but it is possible to construct a document and specify the root element and DocType in one line.  We have named our document <emphasis>profile</emphasis>.
          </para>
          <programlisting>
      DocType type     = new DocType(&quot;html&quot;, &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;, 
                                     &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;);
      profile.setDocType(type);          
          </programlisting>

          <para>
            <emphasis>DocType</emphasis> is an Object that represents a document type declaration, since we are creating an <acronym>XHTML</acronym> document (which is also an <acronym>XML</acronym> document) we specify the <acronym>XHTML</acronym> root element, PUBLIC and SYSTEM identifiers we would like to use by using the constructor:
          </para>

          <programlisting>
  DocType(java.lang.String elementName, java.lang.String publicID, java.lang.String systemID) 
           This will create the DocType with the specified element name and a reference to an external DTD.         
          </programlisting>

          <para>
          Now that our document prologue is sorted we create a new element and set it as the root element of our document, our root element is <emphasis>html</emphasis> like in any other <acronym>XHTML</acronym> document.
          </para>

          <programlisting>
      Element html     = new Element(&quot;html&quot;);
      profile.setRootElement(html);          
          </programlisting>

          <para>We have now actually created within Java the following <acronym>XML</acronym> document.</para>

          <programlisting>
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html&gt;
          </programlisting>

          <para>
            Well actually, the <acronym>XML</acronym> encoding is not determined until we output the document because we can specify a specific encoding (UTF-8 is the default).  But that is trivial, see how easy it is to create an <acronym>XML</acronym> document using JDOM! It is very intuitive. Obviously this document is not complete, nor is it valid <acronym>XHTML</acronym> at the moment because there are elements missing from the inside.
          </para>

          <para>
            The Java code to do this used above is just a single example, there are many other ways to do this and this is not some standard way or anything like that, use whatever way suits your coding style.
          </para>
        </callout>
        <callout arearefs="JDOM-Example-XHML-Maker-GetName">
          <para/>
          <programlisting>
      // get the persons name
      System.out.println(&quot;Hi there, welcome to ProfileGen 1.0&quot;);
      String name = getUserInput(&quot;What is your name?&quot;); 
          </programlisting>
          <para>
            We get the persons name here first so that we can create the page title and header based on it.  It must be noted that this is not entirely necessary, we could create the title elements first and then go back and modify them later, this is why JDOM is so powerful.  If we assume that we have created a <emphasis>head</emphasis> element that contained a <emphasis>title</emphasis> that we wanted to change later we could change it in one line, like this:
          </para>

          <programlisting>
html.getChild(&quot;head&quot;).getChild(&quot;title&quot;).setText(&quot;new text for title&quot;);
          </programlisting>
        </callout>
        <callout arearefs="JDOM-Example-XHML-Maker-Element">
          <para/>
          <programlisting>
      // create head element, title (set by name) and meta (predefined) and styles (predefined)
      Element head     = new Element(&quot;head&quot;);
      head.addContent  ( new Element(&quot;title&quot;).setText(name+&quot;'s Profile&quot;));
      Element meta     = new Element(&quot;meta&quot;);
      meta.setAttribute( new Attribute(&quot;http-equiv&quot;, &quot;Content-Type&quot;));
      meta.setAttribute( new Attribute(&quot;content&quot;, &quot;text/html; charset=utf-8&quot;));
      head.addContent(meta);
      Element style    = new Element(&quot;style&quot;);
      style.setAttribute( new Attribute(&quot;type&quot;, &quot;text/css&quot;));
      style.setText(&quot;h1 { text-align: center; } &quot; +
                    &quot;div.centered {text-align: center;} &quot; +
                    &quot;div.centered table {margin: 0 auto; text-align: left;}&quot;); 

      head.addContent(style);
      html.addContent(head);
          </programlisting>
          <para>
            We can see the creation of a new element called head which is our <acronym>XHTML</acronym> head element, note that we did not have to call it head, we could have called it <emphasis>blah</emphasis> if we had wanted to but in this example we called it head, the text in the constructor we <emphasis>do</emphasis> want to call head because that is the actual text that will be used in the tag.  There are also constructors for creating elements with namespaces.
          </para>

          <programlisting>
      head.addContent  ( new Element(&quot;title&quot;).setText(name+&quot;'s Profile&quot;));
          </programlisting>

          <para>
            Here we add a <emphasis>title</emphasis> element to the <emphasis>head</emphasis> element and set the title to the value of the persons name plus &quot;'s Profile&quot;, this is all done in one line.
          </para>

          <programlisting>
      Element meta     = new Element(&quot;meta&quot;);
      meta.setAttribute( new Attribute(&quot;http-equiv&quot;, &quot;Content-Type&quot;));
      meta.setAttribute( new Attribute(&quot;content&quot;, &quot;text/html; charset=utf-8&quot;));
          </programlisting>

          <para>
          We would also like a meta element to reside within the head element so we create one and set it' attributes, using the intuitive method: <emphasis role="strong">setAttribute(java.lang.String name, java.lang.String value)</emphasis>.
          </para>

          <para>
            The <emphasis>meta</emphasis> element is then added to the <emphasis>head</emphasis> element.  We then create a <emphasis>style</emphasis> element because we want to try and be <acronym>XHTML</acronym> compliant and avoid using <emphasis>align=&quot;center&quot;</emphasis>, however, for various reasons this is a mess, this has nothing to do with JDOM. So then we add our messy <emphasis>style</emphasis> element to the <emphasis>head</emphasis> element and since this is now complete we add the <emphasis>head</emphasis> element to the (root) <emphasis>html</emphasis> element.
          </para>
        </callout>
        <callout arearefs="JDOM-Example-XHML-Maker-Table">
          <para/>
          <programlisting>
      // create table and add profile items to it
      Element table       = new Element(&quot;table&quot;);
      table.addContent(createTableEntry(&quot;Name&quot;, name));
      table.addContent(createDefinedTableEntry(&quot;Age&quot;,&quot;How old are you?&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Sex&quot;,&quot;What is your sex? (M/F)&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Country Of Origin&quot;,&quot;What is your country of origin?&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Occupation&quot;,&quot;What is your occupation?&quot;)); 
      table.addContent(createDefinedTableEntry(&quot;Email&quot;,&quot;What is your email address?&quot;)); 
          </programlisting>
          <para>
          The program makes a table which it adds to the document that contains details about a person, these details are provided by the user prompted by questions at the command line.  It is pretty straightforward, we create a new table element and then add lots of entries to it by calling some methods defined in the class.  We use the standard <emphasis>addContent</emphasis> method (<emphasis role="strong">addContent(Element element)</emphasis>) of the <emphasis>element</emphasis> class to add the elements returned by the method calls.
          </para>
        </callout>
        <callout arearefs="JDOM-Example-XHML-Maker-MakeFile">
          <para/>
          <programlisting>
      // initialize FileWriter, filename created from persons name
      FileWriter writer = null;
      try {
         writer = new FileWriter(name+&quot;_Profile.html&quot;);
      } catch(Exception e) {}
          </programlisting>
          <para>
          We can write our document to a <emphasis>java.io.OutputStream</emphasis> or a <emphasis>java.io.Writer</emphasis>, we want to output to a <emphasis>java.io.Writer</emphasis> so we create it here, the name we give to the file that will be output to is made up of the persons name and the postfix shown. 
          </para>
        </callout>
        <callout arearefs="JDOM-Example-XHML-Maker-XMLOut">
          <para/>
          <programlisting>
      // initialize JDOM XMLOutputter with 2 spaces indent and newlines on
      XMLOutputter prettyOutput = new XMLOutputter(&quot;  &quot;, true);
          </programlisting>
          <para>
            In the words of the JDOM <acronym>API</acronym> <quote>XMLOutputter takes a JDOM tree and formats it to a stream as XML.</quote>  <emphasis>XMLOutputter</emphasis> has a number of useful formatting parameters that can be set; whether elements should be expanded (&lt;hr/&gt; would become &lt;hr&gt;&lt;/hr&gt; (which is not valid)), whether nested elements are separated by newlines and so on.  We have constructed an <emphasis>XMLOutputter</emphasis> object using the following constructor:
          </para>

          <programlisting>
XMLOutputter(java.lang.String indent, boolean newlines) 
           This will create an XMLOutputter with the given indent that prints newlines only if newlines is true; all whitespace from the element text content is included as well.
          </programlisting>

          <para>
            The above text is ripped straight out of the JDOM <acronym>API</acronym>, it is self explanatory and you can see that we have created our <emphasis>XMLOutputter</emphasis> so that it prints newlines and indents nested elements with two spaces.
          </para>

          <programlisting>
      // output the document to the file (via writer) and close writer 
      try {
         prettyOutput.output(profile, writer);
         writer.close();
      } catch(Exception e) {}
          </programlisting>

          <para>
            Once are document is finished we want to output it to the file we created so we use the <emphasis>XMLOutputter</emphasis> <emphasis role="strong">output</emphasis> method: <emphasis role="strong">output(Document doc, java.io.Writer out)</emphasis> to output our document to the <emphasis>FileWriter</emphasis> we created.  The writer is then closed and we are done.
          </para>

        </callout>
        <callout arearefs="JDOM-Example-XHML-Maker-TableEntry">
          <para/>
          <programlisting>
   // creates a html table entry element which embodies identifier and description
   private static Element createTableEntry(String identifier, String description) {
     Element tr     = new Element(&quot;tr&quot;);
     Element td1    = new Element(&quot;td&quot;);
     Element td2    = new Element(&quot;td&quot;);
     Element strong = new Element(&quot;strong&quot;);
     strong.setText(identifier+&quot;: &quot;);
     td1.addContent(strong);
     td2.setText(description);
     tr.addContent(td1);
     tr.addContent(td2);
     return tr;
   }
          </programlisting>
          <para>
            This is one of the methods that is called from the <emphasis>main</emphasis> part of the Java file, it returns an <emphasis>Element</emphasis> object and in this case it takes two strings; an identifier and a description and creates a table entry based on them where the identifier is wrapped in a <emphasis>strong</emphasis> element.
          </para>
        </callout>
      </calloutlist>

      <para>So, as you can see creating <acronym>XML</acronym> documents with JDOM is very simple and intuitive.</para>
      <note>
        <para>
          We have only touched on the features available using JDOM.  JDOM has numerous methods for navigating the JDOM tree such as <emphasis role="strong">getRootElement()</emphasis> which gets the root element of a <emphasis>Document</emphasis>,  <emphasis role="strong">getChildren()</emphasis> which returns a (live, aka modifiable)<emphasis>List</emphasis> of all the child elements nested directly(one level deep) within an <emphasis>Element</emphasis> and <emphasis role="strong">getNameSpace()</emphasis> which will return the <emphasis>Element</emphasis>'s namespace. There are loads more, with a bit of recursion JDOM tree navigation and manipulation can be done easily and efficiently. The details are all there in the JDOM <acronym>API</acronym>
        </para>
      </note>
    </sect2>
  </sect1>

  <sect1 id="References"><title>References</title>
    <itemizedlist>
      <listitem>
        <para><ulink url="http://www.jdom.org/docs/apidocs/">http://www.jdom.org/docs/apidocs/</ulink></para>

        <para>
	         <literallayout>
The JDOM API
Copyright © 2002 Jason Hunter, 
Brett McLaughlin. All Rights Reserved. 
          </literallayout>
	       </para>
      </listitem>

      <listitem>
        <para><ulink url="http://www.servlets.com/speaking/jdom-javaone.pdf">http://www.servlets.com/speaking/jdom-javaone.pdf</ulink></para>

        <para>
	         <literallayout>
JDOM Makes XML Easy
Jason Hunter
Co-Creator
JDOM Project
          </literallayout>
	       </para>
      </listitem>

      <listitem>
        <para><ulink url="http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html">http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html</ulink></para>

        <para>
	         <literallayout>
Easy Java/XML integration with JDOM, Part 1
By Jason Hunter and Brett McLaughlin
          </literallayout>
	       </para>
      </listitem>
    </itemizedlist>
  </sect1>
</article>

